pierrotlefou
pierrotlefou

Reputation: 40721

How to Display Content with Raw HTML with Ruby on Rails

@post.body has following content (which is converted from Markdown by using RDiscount).How should I render it to the user in what it means? i.e I want to render it as strong text emphasized text...

<p><strong>strong text</strong> </p> <p><em>emphasized text</em> </p> <blockquote>  <p>this is a quote</p> </blockquote><p><img src="http://www.picturehouse.com/titles/images/rock.jpg" alt="alt text" title="" /> </p> 

Using <%= @post.body => will only display it as the text shown above.

Upvotes: 27

Views: 37442

Answers (4)

CTS_AE
CTS_AE

Reputation: 14783

Quick, Easy, & to the Point

<%== @post.body %>

More Information

<%== @post.body ==> is an alias to <%= raw(@post.body) ==>

https://edgeguides.rubyonrails.org/active_support_core_extensions.html#output-safety

Upvotes: 3

mikej
mikej

Reputation: 66263

Assuming Rails 3, use the raw helper method e.g.

<%= raw(@post.body) %>

Escaping HTML output is on by default in all view templates (in contrast to earlier versions where you had to use the h method to escape strings individually.)

Upvotes: 57

Jaime Bellmyer
Jaime Bellmyer

Reputation: 23307

I take it you're in Rails 3? One big change is that displayed text used to be raw by default, and you had to sanitize it yourself. Now it's the other way around. Call it like this:

<%= raw(@post.body) %>

And you'll get what you're looking for.

Upvotes: 5

alex.zherdev
alex.zherdev

Reputation: 24164

Are you using rails 3? It automatically escapes all contents of <%= %> tags. To avoid it, do

<%= raw(@post.body) %>

Upvotes: 5

Related Questions