Using kramdown in Rails 3
I'm trying to add some Markdown styling to my Rails 3 blog application. This should be something simple, but I can't get it to work.
I have kramdown
in my Gemfile:
gem 'kramdown'
I ran bun开发者_JAVA技巧dle install
. I have an application helper called kramdown
module ApplicationHelper
def kramdown(text)
require 'kramdown'
return Kramdown::Document.new(text).to_html
end
end
And in my view script:
<%= kramdown(@post.body) %>
I'm getting the error message:
no such file to load -- kramdown
What am I doing wrong?
I've just started experimenting with Kramdown.
Got it working simply by adding to gem file, bundle install, then putting the following in my view.
<%= Kramdown::Document.new(@project.body).to_html %>
Reckon your problem is the require 'kramdown' line. With Rails this is probably getting added behind the scenes. I suspect the kramdown documentation is more focused on Ruby than Rails specifically. The dev server did need restarting too.
Using your suggestion of making a helper, the following works for me.
Application helper:
def kramdown(text)
return Kramdown::Document.new(text).to_html
end
View:
<%= kramdown @project.body %>
I did run into a problem though. All of kramdown's HTML code was visible as Rails was making the HTML safe. To solve this I added sanitize to the helper function:
def kramdown(text)
return sanitize Kramdown::Document.new(text).to_html
end
精彩评论