user1730335 user1730335 - 7 months ago 14
Ruby Question

Using helper method and Ruby enumerable to return html formatted objects

Apologies for the noob question, but I can't seem to figure out how to return formatted HTML objects using a ruby enumerable and a helper. I want to remove the brackets. Is there anyway to return formatted objects with an enumerable based on an array of objects?

Helper

module PostsHelper


def display_posts(posts)
if posts.any?
raw(@posts.map {|p| raw("<div class='post_text_box'>#{p.body}</div>")})
end

end


end


HTML.erb

<div id="post_feed">
<%=display_posts(@posts)%>

</div>


Browser Output

picture of array of formatted html

Answer

You should join your array into whole string before passing it in your view. Try:

if posts.any?
  @posts.map {|p| "<div class='post_text_box'>#{p.body}</div>"}.join.html_safe
end
Comments