Search code examples
ruby-on-railseditortext-editorredcloth

How to convert RedCloth.to_html back into editable vanilla text?


I have with RedCloth saved plain text in a form and converted it to HTML. For example, writing this in my form, and saving it, would make it display the exact same way I wrote it :

This sentence
gets inserted into it
proper html syntax
to preserve line breakage.

With this code :

def parse_code
  self.text = RedCloth.new(text).to_html
end

And then I can redisplay it with this :

= raw post.text

But when I want to edit it, it it returns to me as :

<p>This sentence</p>
<p>gets inserted into it</p>
<p>proper html syntax</p>
<p>to preserve line breakage</p>

How can I make it, so that when I edit it, it looks the same way it did before I went and saved it ?

Thanks!


Solution

  • I would leave the textile code stored in textile and do the conversion to HTML only in the view:

    = raw RedCloth.new(@post.text).to_html
    

    Converting between textile and HTML does not feel to be a good practice. Your parse_code method seem that it caused your text to be converted to HTML.. and than stored to the Db.

    However if you want to convert HTML to textile, maybe clothred is for you or read this blog.