0

I think I'm missing something somewhere.

I had the renderer working for some time with the original text area (content) and have now added a new column to the model (body). I have added everything and the form works, the views show the body input but markdown won't render.

So this is my application helper:

def markdown(content)
 @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, space_after_headers: true, fenced_code_blocks: true)
 @markdown.render(content)
end

def markdown(body)
 @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, space_after_headers: true, fenced_code_blocks: true)
 @markdown.render(body)
end

def title(page_title)
 content_for :title, page_title.to_s
end

And my show view:

=title @portfolio.title 

.container.pushdown.img-responsive
 .row
  .col-md-2
        %br
        %p= link_to 'Back', portfolios_path
    .col-md-8
        %h2
            = @portfolio.title

        %p
            =markdown(@portfolio.body).html_safe
        %p
            =markdown(@portfolio.content).html_safe
        %br
        %br

And I get the following error:

wrong argument type nil (expected String)

2 Answers2

1

Your markdown method expects a string. If you call it with nil, it throws this error.

You may want to change you code to something like this to handle nil values:

def markdown(string)
  @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, space_after_headers: true, fenced_code_blocks: true)
  @markdown.render(string.to_s)
end

Furthermore you have two identical methods. You can delete one of them.

spickermann
  • 100,941
  • 9
  • 101
  • 131
0

The error may come from

=title @portfolio.title

and have nothing to do with markdown rendering. The expected signature for call the function is:

def title(page_title)

Also, +1 to having two identical methods (first one is simply overriden by second.)

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160