5

I'm using RDiscount but my Ruby on Rails skills are limited. RDiscount has its .to_html function which converts the Markdown text into HTML. So here's the scenario:

<% @posts.each do |post| %>
<h3><%= post.title %></h3>
<%= post.content %>
<% end %>

post.content is what I want to convert to html.

1) Where should I create a method to convert the string into HTML?
2) How do I stop RoR from escaping the HTML that RDiscount.to_html returns?

Emil Ahlbäck
  • 6,085
  • 8
  • 39
  • 54

1 Answers1

11

1) Preferably, in a helper 2) By calling html_safe on the resulting string

I have not used markdown in a Rails 3 app, which escapes content by default, but created a helper similar to the h method prior Rails 3, which converted the markdown to html. The approach for Rails 3 would be something like

module Helper
  def m(string)
    RDiscount.new(string).to_html.html_safe
  end
end

in the view

<% @posts.each do |post| %>
  <h3><%= post.title %></h3>
  <%= m post.content %>
<% end %>
Chubas
  • 17,823
  • 4
  • 48
  • 48