How to textilize on Rails 6 comments
The last few days I found different solutions to use textile in Rails application. Let’s have a look to the different ways.
Requirement: RedCloth installation
First you will need to install RedCloth (generally already needed to install Ruby and Gem)
$ sudo gem install redclothSolution 1: RedCloth textilizer
The first way to manipulate textile is to use the RedCloth methods.
If you have a field in your model you want to textilize, by example page.body
textile_page = RedCloth.new(page.body)and then you get the html from
textile_page.to_htmlfor more information you can refer to the RedCloth API
Solution 2: textilize() helper
Rails brings an helper to do the previous work, this time in your view, just call for:
textilize(page.body)Solution 3: acts_as_textiled
The last solution provide a useful way to manipulate automatically textiled content and text content.
Fist add to your model :
class Page < ActiveRecord::Base
acts_as_textiled :body
endThen you can use a form to manipulate automatically your content and saving it to the page.body
After that, if you get the content your page by calling the method page.body you will receive the html. If you want to get the original content just call page.body_source, this method is automatically generated by acts_as_textiled as the method page.body_text returning the text without markup.
Editor: Textile Editor Helper (TEH)
From the slate CMS project, it’s simply a visual editor providing button to apply style to your text.
$ script/plugin install http://svn.webtest.wvu.edu/repos/rails/plugins/textile_editor_helper/[update] I didn’t get before, thanks Dave, you have to load also prototype.js in your layout.
[update 2] Once you have installed your plugin, don’t forget to restart your server, thanks John :)
Then add to your view in a form:
<%= textile_editor 'page', 'body' -%>
<%= textile_editor_initialize -%>Links
Trackbacks
Use the following link to trackback from your own site:
http://www.rubynaut.net/articles/trackback/14171





Is HTML that hard that we need yet another markup language?
Just a note on the textile editor, prototype is required so that the toolbar properly loads.
One benefit of using textilize is that it prevents the content editor from breaking the html markup…
For noobs (like me), don’t forget to restart your web server after installing redcloth :)
so remember kids! sanitize your text befor textilitzing!