I use foo
helper function in my view:
<%= foo ["hello", "stack", "overflow"] %>
When foo
is defined like this:
def foo(arr)
result = ''
arr.each do |a|
result += content_tag(:div, a)
end
result
end
The page renders:
<div>hello</div><div>stack</div><div>overflow</div>
But, if change foo
's definition to be:
def foo(arr)
content_tag(:div, arr[开发者_C百科0]) + content_tag(:div, arr[1]) + content_tag(:div, arr[2])
end
I get the expected result:
hello
stack
overflow
How would you fix foo
's definition above to get the expected result ? (i.e. I don't want the characters to be escaped)
Try this:
def foo(arr)
result = ''
arr.each do |a|
result += content_tag(:div, a)
end
raw result
end
Edit.
To be clearer, you're creating a string and Rails doesn't know whether or not it's safe to display. To be even more precise, Rails has no doubt concerning the content_tags it creates.
So you could solve your problem telling rails your initializer string is safe:
def foo(arr)
result = ''.html_safe
arr.each do |a|
result += content_tag(:div, a)
end
result
end
精彩评论