开发者

How can I concatenate output without auto-escaping image_tag?

I have a column in my HTML table with negative and positive numbers. If the number is negative, I display a green arrow next to the number. If it's positive, I display a red arrow next to the number.

The neatest way I can think of doing this is to make a helper called show_with_arrow where I pass in the number. I'm trying to make the helper method pass back something like -6 ⇓ or 10 ⇑, the arrows being images.

In my show view:

<td><%= show_with_arrow keyword.compare_to(7.day.ago) %></td>

In my helper class:

def show_with_arrow(position_change)
    if position_change > 0
      "#{position_change} #{image_tag('bad_arrow.gif')}"
    elsif position_change < 0
      "#{position_change} #{image_tag('good_arrow.gif')}"
    else
      position_change
    end
end

And it's outputting:

-6 <img 开发者_JAVA百科alt="Good_arrow" src="/images/good_arrow.gif?1295578152" />

instead of

-6 ⇓


I think you need to use raw() like so:

raw("#{position_change} #{image_tag('bad_arrow.gif')}")

You're on rails 3, right?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜