Setting up rich text support for Rails on MySQL and Heroku

What is needed to save the body of formatted text containing bold, bullet points and different sizes in Rails?

First, does the type of field have the textability to hold this?

If so, is it true that all I have to do is edit with a text editor and display with <pre>?

+3
source share
1 answer

Yes, the type is textdesigned to store character strings.

The usual approach is to use a Javascript text editor that generates HTML. You save raw HTML directly to the database, and then sanitize it when it is displayed. This is to ensure that people do not enter Javascript and other nasty things in a text area that could be executed when other visitors view their input.

Rails 3 deactivates your default output, so all HTML will be escaped. You will need to call either <%= @model.rich_text.html_safe %>to skip this disinfecting call (or much better!) <%= sanitize(@model.rich_text, :tags => %w(b i p)) %>, Passing an explicit list of allowed tags.

I have no experience with Heroku, but I cannot imagine that it will be any different.

+4
source

All Articles