Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rails, how can I allow some html in a text area?

I have a Rails app (blog) that I am creating. Very basic stuff. In my content area I have a text area for the content of the post. I am needing to include some html in the text area (links, formating, etc).

<%= f.text_area :content %>

Is there another tag that I can use instead of text_area, that will allow me to do this?

like image 294
Norm Avatar asked May 14 '10 00:05

Norm


4 Answers

The HTML safe method is actually .html_safe. Just tested on a text field.

For example:

<%= @item.description.html_safe %>
like image 152
Mike Avatar answered Nov 05 '22 08:11

Mike


Have you tried this in the views?

 <%= content.html_safe %>
like image 35
MorningHacker Avatar answered Nov 05 '22 08:11

MorningHacker


This is an old question but I guess there's a pretty straight forward view helper for this: sanitize. I presume you'd want to render the HTML tags entered by the user. For that purpose, save the content as a string and render as HTML using sanitize.

Example usage:

sanitize @post, tags: %w(strong em a code pre h2 h3 p blockquote ul ol li br),
                attributes: %w(href class)

The tags option allows you to specify which tags to use and same with the html attributes.

like image 14
mansoor.khan Avatar answered Nov 05 '22 08:11

mansoor.khan


Are you looking for something similar to the bold, italic, ... options you get when posting in stackoverflow? If so, I would suggest Markitup, a text-editor plugin for jQuery. Once you get this set-up, you'll be able to enter mark up in your text area (e.g. Markdown, bbcode, ...). When you actually display the result on the page, you simply need to have Ruby parse the mark up language you chose.

E.g.

<%= @user.bio.bb_code %>

Using this method, you allow your users enter styled text in a safe fashion.

like image 1
ghayes Avatar answered Nov 05 '22 06:11

ghayes