Rails 3.0使用application_helper中的sanitize和simple_format

时间:2012-12-23 04:50:24

标签: ruby-on-rails ruby-on-rails-3

在我看来,我想用p i br等安全的html标签渲染(并截断)某个段落,我使用此代码:

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

它使用html标记呈现段落。

但是当我将该代码传递给我的application_helper

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

有了这个观点

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

呈现

< p>My paragraph < /p> 

如何解决?有没有更好的方法来呈现带有安全标签的段落?

1 个答案:

答案 0 :(得分:2)

一些方法:

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