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.
<one%gt;<br><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
精彩评论