27

@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.

1
  • Since no post mentions it you can use <%== @post.body %> which is an alias to <%= raw(@post.body) %> edgeguides.rubyonrails.org/… Commented Feb 12, 2019 at 20:20

4 Answers 4

57

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.)

Sign up to request clarification or add additional context in comments.

2 Comments

Old thread, but how can you limit what tags are displayed? Ex block <script> and <meta>
Take a look at the sanitize method or check out Ryan Groves's Sanitize library if you need more control.
5

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

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

Comments

5

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.

Comments

4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.