Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 447
pierrotlefou Avatar asked Sep 07 '25 18:09

pierrotlefou


2 Answers

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

like image 181
mikej Avatar answered Sep 09 '25 20:09

mikej


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

<%= raw(@post.body) %>
like image 45
alex.zherdev Avatar answered Sep 09 '25 22:09

alex.zherdev