Rails View Helper does not embed HTML in page

I have a problem using a custom helper method in a Rails (3.0) application to output the required html.

I have the following call in my partial view: _label.html.erb

<% display_resource "Diamond", @resource.diamond %> 

And in the resource_helper.rb file:

 module ResourceHelper def display_resource(display_name, value) "<tr><td>#{display_name} </td><td>#{value.to_s}%</td></tr>" if value > 0 end end 

Estimated Conclusion:

 <tr> <td>Diamond</td> <td>15%</td> <tr> 

* provided without formatting, and 15 is arbitrary

If I use <% = ...%> when making a method call, it will output the string correctly, but it will not be html (i.e. I will see "<tr><td>Diamond </td><td>15%</td></tr>" unlike" Diamond 15% ")

What am I doing wrong?

+6
ruby-on-rails html-helper
source share
1 answer

You need to mark the string returned as " raw " and then use <% =%>

 module ResourceHelper def display_resource(display_name, value) raw("<tr><td>#{display_name} </td><td>#{value.to_s}%</td></tr>") if value > 0 # string wrapped in raw end end 
+7
source share

All Articles