Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RDiscount, where should I do the actual formatting?

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?

like image 468
Emil Ahlbäck Avatar asked Feb 09 '11 17:02

Emil Ahlbäck


1 Answers

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 %>
like image 133
Chubas Avatar answered Oct 18 '22 15:10

Chubas