Displaying markdown in my textbox

I am using BlueCloth to generate markdown html from content my users enter into the text space, for example:

def create
  @post = Post.new(params[:post]) do |post|
    body = BlueCloth.new(post.body) 
    post.body = body.to_html
  end

...

end

      

This works great! I get the html stored in the database fine, but how do I show the markdown in the textbox when the user is editing? I tried:

def edit
  @post = Post.find(params[:id])
  @post.body = BlueCloth.new(@post.body)
  @post.body.text
end

      

The result in my textbox looks like this:

#<BlueCloth:0x10402d578>

      

+2


source to share


1 answer


Bluecloth documentation is not well defined. I'm not sure if there is an easy way to convert html => markdown.

However, there is nothing to prevent you from storing the markdown in your database and converting it to html if necessary.

If you want the html to be returned by default by @ post.body, you can always override it.



class Post < ActiveRecord::Base
  ...
  def body
    BlueCloth.new(@body).to_html
  end

  def markdown
    @body
  end
end

      

Now @ post.body returns the html version of the markdown. while @ post.markdown returns the markdown source.

+2


source







All Articles