开发者

Rails View Helper Not Inserting HTML into Page

开发者 https://www.devze.com 2023-01-18 01:07 出处:网络
I\'m having an issue using a custom helper method in my Rails (3.0) app to output the required html. I have the following call in my partial view:

I'm having an issue using a custom helper method in my Rails (3.0) app 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

The intended output is:

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

*granted, without the formatting, and the 15 is arbitrary

If I use the <%= ... %> when performing the method call, it'll output the string correctly, but it won't be html (ie I'll see "<tr><td>Diamond </td><td>15%</td></tr>"开发者_StackOverflow中文版 as opposed to "Diamond 15%")

What am I doing incorrectly?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消