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 if
upvoted` 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) %>
精彩评论