Greetings!
Today when i was working with helper methods for a ruby on rails project, i faced a problem of not rendering HTML tags in View
Instead it rendered as a String.
For example, the actual scenario is,
I had the following code in helper module
def show_list(num, page=nil)
..........
..........
content = []
for i in collection
content << "<li> #{i.name} </li>"
end
.........
.........
end
Inside view when i called this method
<%= show_list(25) %>
I got the output as a string, “<li>actual data goes here</li>”, not interpreting the HTML tags
Then after surfing for nearly a couple of hours, i came to know that its a wrong way of calling the method in View
The right way is to prefix the method using raw helper method
Correct way
<%= raw show_list(25) %>
Now the view displays the data as a list item
Advertisement
Thank you!!! I’ve been surfing myself and this post is the first time I came across this solution.
Thanks for the solution..
I faced this problem and it helped me out.
Mani
unfortunately still not the right way :-p
meet the content_tag helper
http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag
You really should use .html_safe to render the HTML as proper tags on the webpage – so in your example:
show_list(25).html_safe instead. Your commenter is removing all markup.