Ruby: 2.0.0p0, Rails: 3.2.13,redcarpet: 2.2.2application_helper.rb
def markdown(text)
markdown_render = Redcarpet::Render::HTML.new(:hard_wrap => true, :no_styles => true)
markdown = Redcarpet::Markdown.new(markdown_render, :autolink => true, :no_intro_emphasis => true)
markdown.render(text).to_html.html_safe
end
app/views/questions/new.html.erb
<%= simple_form_for @question do |f| %>
<%= f.input :title, :input_html => { :class => "span6" } %>
<%= markdown(@question.content) %>
<%= f.button :submit, :class => 'btn-primary' %>
<%= link_to 'Cancel', @question.id.blank? ? questions_path : question_path(params[:question]), :class => "btn btn-danger" %>
<% end %>
但是出现了错误:wrong argument type nil (expected String)
,然后我把<%= markdown(@question.content) %>
改成了<%= markdown(@question.content.to_s) %>
,然后出现了这个错误:undefined methodto_html' for "":String
,所以我把markdown.render(text).to_html.html_safe
改成了markdown.render(text).html_safe
在application_help.rb
中,它只有标题输入字段,内容输入字段丢失了。
我该如何解决这个问题,如果你需要更多的信息,请告诉我。
试试下面的帮助器:
def markdown(text)
if text.blank?
nil
else
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
markdown.render(text)
end
end
这在Rails 4.1.0上对我有效。
将以下内容放入app/helpers/application_helper.rb:
def markdown(text)
if text.blank?
nil
else
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true, filter_html: true, hard_wrap: true, link_attributes: { rel: 'nofollow', target: "_blank" }, space_after_headers: true, fenced_code_blocks: true, superscript: true, disable_indented_code_blocks: true)
markdown.render(text).html_safe
end
end
认为唯一的区别是把.html_safe
放在末尾