Using Markdown with Rails

I want users to type Markdown text in a text box, and when they publish it, I display the corresponding html. I read that Rails used the markdown method or a similar method that you could just call in this field in the ERB file:

 <%= markdown(@post.content) %> 

Rails seems to have taken this functionality. What is the best way to get this functionality again? This seems to solve my need.

+7
source share
2 answers

I would use Redcarpet to convert markdown-html. Also, I would move the transformation from the view to some other object. You can use callbacks ( before_save ) or use Observers .

From the docs :

 markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true) markdown.render("This is *bongos*, indeed.") #=> "<p>This is <em>bongos</em>, indeed</p>" 

You probably want to save the result in another column, say @post.content_parsed , so that the user can make subsequent changes to the message, and also so that you do not need to do the conversion for each request.

+12
source

The solution proposed was turned into a gemstone emd . You can read more here.

Add these lines to your gemfile application:

 gem 'coderay' #optional for Syntax Highlighting gem 'redcarpet' gem 'emd' 

And then do:

 bundle 
0
source

All Articles