开发者

rails 3 html_safe confusion

How do I achieve this?

In a controller

@arr = ["<one>", "<two>"]

In a view (haml)

= @arr.join开发者_开发百科("<br>")

As you guess, "<br>" shouldn't be escaped. So the result would be like the following.

&lt;one%gt;<br>&lt;two%gt;

How do I do that?

Thanks.

Sam


Rails has this built in.

<%= safe_join(@arr, "<br />".html_safe) %>


You could roll your own:

arr = ["<one>", "<two>"]
''.html_safe.tap {|x|
  arr.each_with_index { |el, ix|
    x << el
    x << raw("<br/>") if ix < arr.size-1
  }
}

Also look at the Array.join code in Rails

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜