I have an array in my view file. Each value in the array contains html. I want to go through the array and not escape the html. However, whenever I run html_safe or raw, the array is displayed with non-escaped html. Below are some of the methods I tried that proved a failure.
This returns the array with escaped html…
<%= @posts.each { |x| puts raw(x) } %>
This returns with each value in the array as nil…
<%= @posts.map { |x| puts raw(x) } %>
Finally, inspect returns the array with escaped html…
<%= @posts.inspect { |x| puts x.html_safe } %>
This returns with nil as well…
<%= @posts.map { |x| puts x.html_safe } %>
What’s causing this and how do I fix it?
Your problem is with the
<%=at the start of every example – this will output the result of the expression, which is@postsitself.Replace it with
<%and see how you go.edit: The first example is the one I would use, with each and raw.
edit again: puts really isn’t typical in views – try something like the following: