cdotfeli
cdotfeli

Reputation: 309

Escape <a href> tags while using simple_format

I have an app where I would like to display the line breaks in the content of a text_area.

I know I can accomplish this using simple_format, but for SEO reasons I would also like to disallow them from inserting links into the content.

How do I go about displaying the line breaks, and not render the <a> tags ?

Upvotes: 0

Views: 264

Answers (1)

Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47913

You can combine simple_format with sanitize:

# prints <p>foo\n<br />barbaz</p>
simple_format sanitize("foo\nbar<a href='foo'>baz</a>", :tags => [])

Pass allowable tags as the :tags option (e.g. :tags => ["p", "em"]).

Upvotes: 2

Related Questions