Search code examples
ruby-on-railstextileredcloth

Covert "back" to textile markup when editing post via RedCloth and Acts-as-Textiled?


Question: How can I "convert" my textile generated HTML back to textile markup in my app?

Here's the situtation. I managed to piece together a poor man's blog editor in my Rails 3 app by implementing RedCloth and acts-as-textiled in my "posts" model. I use acts-as-textiled to take the pain out of writing content in the body of my "posts".

It works great. However, when I edit the post I see the generated HTML in the post body. What I would like to see is the original textile markup. Is this possible?

Thanks in advance for your help!

Here is my post model:

class Post < ActiveRecord::Base
  has_many :tags
  acts_as_taggable_on :tags
  acts_as_textiled :title, :body
  attr_accessible :tag_list, :tags, :title, :body, :post, :comments
  validates :title, :presence => true
  validates :body, :presence => true
  default_scope :order => 'posts.created_at DESC'

Here is my Posts view for the "Show" method:

<%= @post.title.html_safe %>
<%= @post.body.html_safe %>

Solution

  • Try this:

    html = <<HTML
    <h1>This is a heading</h1>
    <strong>bold text</strong>
    <i>italic</i>
    HTML
    
    textile = ClothRed.new(html).to_textile
    
    puts textile
    

    Output:

    h1. This is a heading
    
    
    *bold text*  
    __italic__
    

    In your case, I think you should do:

    <%= ClothRed.new(@post.body).to_textile.html_safe %>
    # instead of
    <%= @post.body.html_safe %>
    

    I don't know if this is considered best practice, though.

    Documentation: http://clothred.rubyforge.org/doc/html/index.html