开发者

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

开发者 https://www.devze.com 2023-02-11 19:11 出处:网络
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

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开发者_JS百科 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消