1

Ruby:2.0.0p0,Rails:3.2.13,红地毯:2.2.2
application_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_safemarkdown.render(text).html_safe并且application_help.rb它只有标题输入字段,内容输入字段已丢失。
我该如何解决这个问题,如果您需要更多信息,请告诉我。

4

2 回答 2

3

尝试以下助手:

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  
于 2013-05-07T19:33:11.303 回答
0

这在 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接近尾声

于 2014-07-31T14:38:22.607 回答