Emi*_*äck 5 ruby-on-rails rdiscount ruby-on-rails-3
我正在使用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?
Chu*_*bas 11
1)最好在帮助器中2)通过在结果字符串上调用html_safe
我没有在Rails 3应用程序中使用markdown,默认情况下它会转义内容,但是创建了一个类似于hRails 3之前的方法的帮助程序,它将markdown转换为html.Rails 3的方法就是这样的
module Helper
def m(string)
RDiscount.new(string).to_html.html_safe
end
end
Run Code Online (Sandbox Code Playgroud)
在视图中
<% @posts.each do |post| %>
<h3><%= post.title %></h3>
<%= m post.content %>
<% end %>
Run Code Online (Sandbox Code Playgroud)