Using RDiscount, where should I do the actual formatting?

1.2k views Asked by At

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?

1

There are 1 answers

0
Chubas On BEST ANSWER

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 %>