zjhui
zjhui

Reputation: 809

redcarpet-2.2.2 doesn't come the content

Ruby: 2.0.0p0 , Rails: 3.2.13,redcarpet: 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 %>  

But it comes error:wrong argument type nil (expected String), then i changed <%= markdown(@question.content) %> to <%= markdown(@question.content.to_s) %>, it then comes this error: undefined methodto_html' for "":String, so i changed markdown.render(text).to_html.html_safe to markdown.render(text).html_safe in the application_help.rb and it just has the title input field, the content input field has missed.
How can i fix this problem and if you need more information, please tell me.

Upvotes: 1

Views: 417

Answers (2)

benrmatthews
benrmatthews

Reputation: 131

This worked for me on Rails 4.1.0.

Put the following in 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

Think the only difference is putting .html_safe towards the end

Upvotes: 0

wintermeyer
wintermeyer

Reputation: 8318

Try the following helper:

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  

Upvotes: 3

Related Questions