我正在尝试在这个文本字段中执行if and else语句,其值为:
<%= 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}" %>
我看到这个错误:
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 => '...
... ^
我通常使用缩写,如下所示:
: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}" %>
它最终看起来像这样:value。
:value => (vind.ratings == 0) ? 0 : vind.rating_score/vind.ratings
我可以这样做:
<%= 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}" %>
或类似于上面,但在((中:
<%= 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}" %>