首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >redcarpet-2.2.2没有提供内容

redcarpet-2.2.2没有提供内容
EN

Stack Overflow用户
提问于 2013-05-07 23:02:40
回答 2查看 397关注 0票数 1

Ruby: 2.0.0p0,Rails: 3.2.13,红毯: 2.2.2

application_helper.rb

代码语言:javascript
复制
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

代码语言:javascript
复制
<%= 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,所以我在application_help.rb中将markdown.render(text).to_html.html_safe更改为markdown.render(text).html_safe,它只有标题输入字段,内容输入字段丢失了。

我如何解决这个问题,如果您需要更多信息,请告诉我。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-08 03:33:11

尝试以下帮助程序:

代码语言:javascript
复制
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  
票数 3
EN

Stack Overflow用户

发布于 2014-07-31 22:38:23

这对我在Rails 4.1.0上很有效。

将以下内容放入app/helpers/application_helper.rb:

代码语言:javascript
复制
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放到最后

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16422370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档