Rendering markdown text in Rails 3

喜欢而已 提交于 2021-02-06 09:38:32

问题


I want to convert markdown to html. In my views I have markdown(some_text_variable) but I get the error undefined method markdown.

I added require 'BlueCoth' to enviroment.rb and I installed the BlueCloth gem. Can anybody help me?


回答1:


In your Gemfile:

gem 'bluecloth'  

and don't forget to run

bundle install

when you need to convert markdown to html, simply use:

markdown = BlueCloth.new(some_text_variable).to_html

You can use it in a view:

<%= markdown.html_safe %>



回答2:


You can use RDiscount https://github.com/rtomayko/rdiscount




回答3:


The #markdown helper was removed in Rails 3.

You can copy & paste the code from the commit or use a third party Gem, like formatize. You will also have to include the required BlueCloth/RedCloth/etc gem in your Gemfile.




回答4:


I'd recommend against bluecloth as it doesn't look like it's being upkept that well.

Best thing I've found out there as of now (summer 2013) is the Redcarpet gem: https://github.com/vmg/redcarpet




回答5:


Try adding the following to your environment.rb instead of the require:

config.gem 'BlueCloth', :lib => 'bluecloth'

In Rails 3, this would be in the Gemfile:

gem 'bluecloth'

The gem is called BlueCloth, but the .rb file that gets required is all lowercase.

Make sure you restart rails after adding the config.gem line.



来源:https://stackoverflow.com/questions/3578825/rendering-markdown-text-in-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!