开发者

How do I apply one of two classes to an image depending on a certain condition in Rails 3?

So for one class, I would do something like:

<%= image_tag(upload.image.url, :class => upload.upvoted? ? 'upvoted' : nil) %>

But I want to have it check to see if downvoted applies, and if so applies the class downvoted'. I开发者_运维技巧f it doesn't apply, it checks to see ifupvoted` does, and then applies that class.


I would move the logic for determining the class to a helper method:

apps/helpers/stage_helper.rb

module StageHelper
  def upload_class(upload)
    if upload.upvoted?
      'upvoted'
    elsif upload.downvoted?
      'downvoted'
    end
  end
end

Then your image tag help would be:

<%= image_tag(upload.image.url, :class => upload_class(upload) %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜