基本上,我想做的是检查方法是否在对象上返回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/

10-13 02:13