How to textilize on Rails 6 comments

Posted by stephane Fri, 06 Apr 2007 10:00:00 GMT

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 redcloth

Solution 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_html

for 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
end

Then 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

Comments

Leave a response

  1. Mike about 13 hours later:

    Is HTML that hard that we need yet another markup language?

  2. Dave 1 day later:

    Just a note on the textile editor, prototype is required so that the toolbar properly loads.

  3. Martin Kjems 10 days later:

    One benefit of using textilize is that it prevents the content editor from breaking the html markup…

  4. John D 17 days later:

    For noobs (like me), don’t forget to restart your web server after installing redcloth :)

  5. S2 12 months later:
    hi. I found out that in rails 2.0 (I don’t know if this happened before), that if you textilize, the script tags are not escaped: alert(1); Maybe I got something wrong in my setup. I’ll test it here to see if you have the same bug.
  6. S2 12 months later:

    so remember kids! sanitize your text befor textilitzing!

Comments