5

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

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

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

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

4

1 回答 1

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 %>
于 2011-02-09T17:29:00.547 回答