2

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 %>
zetetic
  • 47,184
  • 10
  • 111
  • 119
thoughtpunch
  • 1,907
  • 4
  • 25
  • 41

1 Answers1

2

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

Thiago Silveira
  • 5,033
  • 4
  • 26
  • 29
  • This works, but I think I would be better off with a drop-in textile editor like MarkItUp rather than converting back and forth from textile every time I view or edit a post. – thoughtpunch Dec 02 '10 at 20:56