如何在Rails 3中的image_tag助手中执行if语句?

mar*_*ion 2 ruby-on-rails ruby-on-rails-3

基本上我想要做的是检查方法是否在对象上返回true,如果是,则将特定类应用于图像.

如果没有,那就什么都不做.

所以我尝试了这个:

<%= image_tag(upload.image.url, if upload.upvoted? :class => 'upvoted') %>
Run Code Online (Sandbox Code Playgroud)

但这是我得到的错误:

/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 ')'
Run Code Online (Sandbox Code Playgroud)

ben*_*sie 6

<%= image_tag(upload.image.url, :class => upload.upvoted? ? 'upvoted' : nil) %>
Run Code Online (Sandbox Code Playgroud)

  • 为什么不创建一个帮助器,将上传变量传递给它,让它返回正确的类? (2认同)