Basically what I want to do is check to see if a method returns true on an object, if it does, then apply a specific class to the image.
If not, then do nothing.
So I tried this:
<%= image_tag(upload.image.url, if upload.upvoted? :class => 'upvoted') %>
But this is the error I get:
/app/views/stages/compare.html.erb:29: syntax error, unexpected ')', expecting keyword_then or ';' or '\n'
...d.upvoted? :class => 'upvoted') );@output_buffer.safe_concat...
... 开发者_JAVA百科 ^
/app/views/stages/compare.html.erb:31: syntax error, unexpected keyword_end, expecting ')'
'); end
^
/app/views/stages/compare.html.erb:63: syntax error, unexpected keyword_ensure, expecting ')'
/app/views/stages/compare.html.erb:65: syntax error, unexpected keyword_end, expecting ')'
<%= image_tag(upload.image.url, :class => upload.upvoted? ? 'upvoted' : nil) %>
As I said in my comment, I would move the logic for determining the class to a helper method:
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) %>
精彩评论