Ricardo Castañeda
Ricardo Castañeda

Reputation: 5812

Rails 3.0 use sanitize and simple_format from application_helper

When in my view, I want to render (and truncate) some paragraph with safe html tags like p i br etc, I use this code:

- @last_testimony.each do |last_testimony|
  = sanitize(simple_format(truncate(last_testimony.description, :length => 25)), :tags => %w(p i br b))

It renders the paragraph with html tags.

But when I pass that code to my application_helper

def paragraph(text, length)
  "#{sanitize(simple_format(truncate(text, :length => length)), :tags => %w(p i br b))}"
end

With this view

- @last_testimony.each do |last_testimony|
  = paragraph(last_testimony.description, 10)

It renders

< p>My paragraph < /p> 

How to fix it? Is there a better method to render paragraphs with safe tags?

Upvotes: 0

Views: 535

Answers (1)

VadimAlekseev
VadimAlekseev

Reputation: 2248

Some ways to do it:

1.

- @last_testimony.each do |last_testimony|
  = raw paragraph(last_testimony.description, 10)

2.

def paragraph(text, length)
  "#{sanitize(simple_format(truncate(text, :length => length)), :tags => %w(p i br b))}".html_safe
end

Upvotes: 2

Related Questions