基本上我想做的是检查一个方法是否在一个对象上返回 true,如果是,则将特定的类应用于图像。
如果没有,那么什么都不做。
所以我尝试了这个:
<%= image_tag(upload.image.url, if upload.upvoted? :class => 'upvoted') %>
但这是我得到的错误:
/app/views/stages/compare.html.erb:29: syntax error, unexpected ')', expecting keyword_then or ';' or '\n'
...d.upvoted? :class => 'upvoted') );@output_buffer.safe_concat...
... ^
/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) %>
关于ruby-on-rails - 如何在 Rails 3 的 image_tag 助手中执行 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5383819/