Rails JBuilder Render HTML Inline - ruby-on-rails

I have a large number of records that need to be rendered to JSON using JBuilder. One of the fields is an HTML representation of the record that is currently using an ERB partial.
Is there a way to render the HTML inline so I don't incur a performance hit by loading and rendering each partial?

Related

Showing html data from database to front end in mvc

I have upgraded my application from asp.net to mvc4. I am using html5 and displaying data in an html table. My database column contains html tags, but it is getting rendered as a plain text. Please help.
**
> Removable hard drive carrier only (DataPort)<html><br><b><font color
> ="red"> Part 444873-001 is no longer supplied. Please order the replacement, 580620-001</font></b></br></html>
**
Above line is a sample of how my data gets displayed in the column. I want to make the html tags to be rendered as html itself.
When you render your model data, use the Html.Raw() helper to render the HTML data unencoded. Razor automatically encodes HTML inputs in order to help prevent XSS attacks on websites.
<td>#Html.Raw(Model.MyProperty)</td>

Rails: JSON URL returning HTML

I have a Rails 4 application, and the URL localhost:3000/resources.json returns and HTML file, instead of JSON. The index.json.jbuilder is generated by Rails scaffold itself. Any idea why the JSON output is going wrong?
Thought I will answer this question myself based on further searches.
HAML prevents template engines to render anything else than HTML
http://denysonique.blogspot.com/2012/03/rails-views-htmlhaml-vs-haml-htmlerb-vs.html

Rails 3 HTML Injection

Currently following the Learning Rails Screencasts at http://www.buildingwebapps.com/learningrails, making any necessary changes to work in Rails 3. However, in the tenth episode, I'm having a problem when rendering html code out of the database. The Page model in the tutorial has a body field, where the html of each page is put. The viewer controller's 'show' method grabs a Page from the database, and yields the contents of #page.body into the view. However, instead of rendering tags such as h1 properly, when I view the html source in the browser my tags are being rendered as <h1;#gt. Is there any way I can fix this?
Just for reference, my 'show' view is as follows:
<%= #page.body %>
Try this:
<%= raw(#page.body) %>
Raw method prevents escaping HTML characters.

Rendering a partial within "<code" or "<pre>" tags with jQuery and Rails

I am working on a simple Rails/jQuery HTML templater app which stores a series of pre-designed templates in a database (at the moment I've just saved these as partials to get the basic concept working) and on clicking 'Show code' alongside any one of these template records, a js.erb script should place the corresponding partial within 'pre' tags dynamically via JS on that page so the user can see the raw html code.
At the moment it's working but I get the rendered html coming back and not the raw HTML that I'm looking for. Here's the js:
$("div#template-view").html("<pre><code><%= escape_javascript( render :partial => "core_template") %></code></pre>");
So pray tell, what obvious thing am I missing!? :-)
Thanks
Allan
Use
$("div#template-view").text("...")
instead. This will not parse the code
The pre tag will show source code (or any text) in a reasonable approximation to it's original state, but it won't escape html for you. Unescaped html will always be rendered as html regardless of what tag it happens to be in. By escaped i mean that all the special characters are converted to their escaped versions. The rails method h will do this for you, so if you call h with the results of calling escape_javascript then it should work fine.
$("div#template-view").html("<pre><code><%= h(escape_javascript(render :partial => "core_template")) %></code></pre>");

HTMl Helpers in MVC: How do I render html tags with helper such as ActionLink?

When using ActionLink to render data from database which has HTML tags
(ie <p>)
incorporated in it, ActionLink escapes the tags. What is the best way to handle this?
In valid (X)HTML, paragraph tags are disallowed inside of anchor tags, so I wouldn't expect the framework to allow it.
I don't know that you can turn off the XSS protection in the helper methods, but you can always build your own helper methods. Just make an extension method that hangs off the Html class.
If you just want to render some HTML from the database, you can use <%= ViewData["MyContent"] %> if you're controller loads the data into the MyContent view data. Just know that you have to clean this HTML yourself.

Resources