ChrisWesAllen
ChrisWesAllen

Reputation: 4975

Ruby/Rails - Parsing and Displaying HTML from a Models Attribute

I have a Rails Model called Events which has as field/attribute called :description.

I migrated it under the type t.text rather than t.string since I was going to be displaying a large amount of data.

So.... Now I'm at the point where I would like to display <%= @event.description %> in a neat way and would like to break up some of the sentences rather than one large block of information.

I was hoping I could insert <p> or other html codes to help with how the text is displayed.

The problem is inserting <p> into the field only prints <p> and the not desired action of the html command.

How can I add html styling to the text attribute?

Upvotes: 0

Views: 1634

Answers (2)

Chris Heald
Chris Heald

Reputation: 62638

You can use <%=raw @event.description %> to echo unescaped content. Be aware that this is a potential XSS security hole, if users can ever affect that content. I highly recommend using the sanitize helper to strip out any unwelcome markup before you write it out.

Upvotes: 8

Nikita Rybak
Nikita Rybak

Reputation: 67986

It strictly depends on how you print it. In particular, if you print with calling h function (<%= h my_text =>), output will be sanitized and html escaped.

It may also depend on your rails version: I've head in Rails 3 html is escaped by default, even if you don't use h.

Upvotes: 0

Related Questions