Rails generate html file from html.erb template - html

I know thas it sounds a little weird. I need to generate an html file based on an html.erb template which has many variables. I will give an oversimplified example.
In my viwes/pages/home.html.erb I have:
<html>
<h1> <%= #name %> </h1>
</html>
In my pages_controller.rb:
def home
#name = 'Michaela'
end
When the home funcion is called, it will render the home.html.erb and in the browser, the content will be showed as if it was opnening an .html file that looks like the following:
<html>
<h1> Michaela </h1>
</html>
What I need is to generate an plain html file with that content, after #name has been defined, so if instad of being hardcoded, it´s inputted by the user, the .html file defines it´s content accroding to that user input.

As Maxence recommended, I used the render_to_string helper to finally store the generated html into a variable, and manipulate it in that way.

Related

insert JSP file into other JSP file to specific div

Is that possible? For example, if I have my main.jsp that is just a container/template in which other JSP files get included, to their specific predefined places.
Something like the following:
<body>
<!-- user input -->
<!-- Insertion point for user input template -->
<div id="user_input">I want user_input.jsp here</div>
<!-- Insertion point for user data template -->
<div id="user_data"> I want user_data.jsp here</div>
</body>
So if I just include files user_input.jsp and user_data.jsp on the top of the page like <%# include file="user_input.jsp" %> then those HTML elements won't be in place. So in user_input.jsp I have the HTML input form, and I want it to be exactly where the <div id="user_input"> is.
How to achieve that?
What you can do is use a JSP fragment (.jspf). Which can put in a fragment of JSP into a specific place in the file.
You can input a .jspf into your jsp file using this:
<%# include file="/path/to/file.jspf" %>
To convert your .jsp files into .jspf, you just put an f at the end, and you may need to reformat your code as if you we're putting it into main.jsp
(eg: removing imports, remove unneccesary HTML tags...etc)
JSP fragments are just JSP files but are pretty much combined with your .jsp file, so any imports that you need you may need to do on your main.jsp.
Hope this helps

MVC Razor View: Output text as both raw and HTML parsed?

I am having a hard time figuring out how to do this...
I am essentially saving a huge blog post in a property called "Body" in a class called "Post". In body I will have various things like
<p> Hello world </p>
<p> Some random paragraph </p>
<codeblock> Here is an example of a basic HTML page
<html>
<body>
<h1> Hello Guys ! </h1>
</body>
</html>
</codeblock>
Then I want to have a code block and thus I want the HTML/CSS/Javascript/etc to just be parsed to the page as HTML encoded/decoded so I literally want the tags and angle brackets to show up on the page instead of being parsed as whatever they are.
I also have a HTML tag called which is ended by . It's nothing special it just indents and adds some specific CSS with it. I want the markup before the and after the tag to render the HTML tags as necessary.
Currently I am literally outputting the contents of the Body property using
#Html.Raw(post.Body)
Nothing special when I save it to the DB:
#Html.TextAreaFor(model => model.Body)
So for those that are still having this issue, this is how I resolved it.
1) I included prettyprint. See link below
https://google-code-prettify.googlecode.com/svn/trunk/README.html
2) When editing a post I just add the following code
//Bunch of code
Example:
var http = require("http");
var server = http.createServer(function(req, res){
console.log(req.url);
resp.write("<html><body>" + req.url + "</html></body>");
resp.end();
});
server.listen(3000);
</pre>
In my Razor View I have the following code:
<div class="blog margin-bottom-40" onload="prettyPrint()">
//Bunch of other code up here for my view
<div class="blogpost">
#Html.Raw(post.Body)
</div>
</div>
My blog is www.techiejs.com
Feel free to have a look and if you need another file from my solution let me know. Currently my git repository is private.

Displaying some HTML code in my Rails view

In my controller, I'm outputting some HTML that I want to give to the user to copy but not have that HTML render what it actually renders on the page.
Here's the code from my show action:
#url = <iframe src=#{url} scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden;width:800px; height:21px;\" allowtransparency=\"true\" ></iframe>
What should I do differently so the view will display the HTML properly, the user can just copy the whole thing, paste it and be good to go?
You need to escape your HTML, and you can use html_escape() or aliased method h() to do this. In your view, where you currently have something like:
<%= #url %>
change to:
<%=h #url %>
For further info: http://api.rubyonrails.org/classes/ERB/Util.html#method-c-html_escape
in your view try this:
<%=raw(#url)%>
some usefull information can be found here:
http://www.tigraine.at/2012/08/23/when-to-use-raw-and-when-to-use-html_safe

html.erb doesn't recognize html tags?

New to RoR, so please don't kill me ;)
Was wondering why does not Rails 3 recognize HTML tags retrieved from database?
For example,
Name Content
Title <b>Great</b> Show Edit Destroy
I wanted to have Content to be bold and put < b > tag around it, when it retrieves from a database it looks like a plain English.
Any thoughts?
Thank you in advance.
If I got this right, you need to do this:
<%= myrecord.content.html_safe %>
to get "real" html and not just escaped html code.
Even though it is unsafe to deliberately output HTML from the DB, you should call raw on the content you're trying to print.
<%= raw #object.my_content %>

interpreting escaped html

I am using rails 3.0 and I have an xml file where I store the content of my webpage. So for example, to fill in the body section of a given html page, I extract the content of the tag using REXML methods in ruby.
I would like to store a content with HTML tag inside this XML tag. Say, the following is my favorite content:
<body><strong>XXX</strong></body>
I am inserting this text in its escaped version so that XML parser doesn't interpret it as some content.
"<strong> XXX </strong>"
Running seeds.rb file, I am reading this content to the database and eventually render it as an html page.
I tried many methods, I was unable to obtain what I want, namely: XXX
thanks for your help.
Another easy to output raw content
<%== #content.body %>
It's exactly the same as
<%= raw #content.body %>
it's just a shorthand method to output raw content
try putting the html inside a CDATA section (unescaped):
<xmlNode>
<![CDATA[
<body><strong>XXX</strong></body>
]]>
</xmlNode>
solved the problem, in my XML file, I have this content entered unescaped as it is:
<body>
<strong>XXX</strong>
</body>
in my seeds.rb file, I read the child of node with:
k.children[1].to_s
and finally in the controller, I declare the content as .html_safe:
<%= content_tag(:div, #content.body.html_safe) %>