使用RDiscount,我应该在哪里进行实际格式化?

时间:2011-02-09 17:13:24

标签: ruby-on-rails ruby-on-rails-3 rdiscount

我正在使用RDiscount,但我的Ruby on Rails技能有限。 RDiscount具有.to_html函数,可将Markdown文本转换为HTML。所以这是场景:

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

post.content是我想要转换为html的内容。

1)我应该在哪里创建一个将字符串转换为HTML的方法?
2)如何阻止RoR转义RDiscount.to_html返回的HTML?

1 个答案:

答案 0 :(得分:11)

1)最好是在帮助者中 2)在结果字符串上调用html_safe

我没有在Rails 3应用程序中使用markdown,它默认会转义内容,但是在Rails 3之前创建了一个类似于h方法的帮助程序,它将markdown转换为html。 Rails 3的方法类似于

module Helper
  def m(string)
    RDiscount.new(string).to_html.html_safe
  end
end
视图中的

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