开发者

Rails - How to do If and else statements in a text field

I am trying to do a if and else statement in this text field with the value:

<%= f.text_field :rating, :size => 1, :min => 1, :max => 6, :value => if vind.ratings == 0 else vind.rating_score/vind.ratings, :type => 'range', :step 开发者_如何学C=> '1', :id => "#{vind.id}" %>

I get this error in view:

SyntaxError in Kategoris#show

Showing C:/Rails/konkurranceportalen/app/views/kategoris/_rating.html.erb where line #3 raised:

C:/Rails/konkurranceportalen/app/views/kategoris/_rating.html.erb:3: syntax error, unexpected keyword_else, expecting keyword_then or ';' or '\n'
...ue => if vind.ratings == 0 else vind.rating_score/vind.ratin...
...                               ^
C:/Rails/konkurranceportalen/app/views/kategoris/_rating.html.erb:3: syntax error, unexpected ',', expecting ')'
...vind.rating_score/vind.ratings, :type => 'range', :step => '...
...                               ^


I usually use the shorthand, like this:

:value => ( vind.ratings == 0 ? 0 : vind.rating_score/vind.ratings )


<%= f.text_field :rating, :size => 1, :min => 1, :max => 6, :value => (vind.ratings == 0) ? 0 : vind.rating_score/vind.ratings, :type => 'range', :step => '1', :id => "#{vind.id}" %>

Which ends up looking like this for :value.

 :value => (vind.ratings == 0) ? 0 : vind.rating_score/vind.ratings


I can do like this:

<%= f.text_field :rating, :size => 1, :min => 1, :max => 6, :value => "#{if vind.ratings == 0 else vind.rating_score/vind.ratings, :type => 'range', :step => '1', :id => "#{vind.id}" %>

or like above but in ():

<%= f.text_field :rating, :size => 1, :min => 1, :max => 6, :value => ((vind.ratings == 0) ? value_here : vind.rating_score/vind.ratings), :type => 'range', :step => '1', :id => "#{vind.id}" %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜