【发布时间】:2016-02-08 21:42:12
【问题描述】:
我有一对单选按钮,我想将 checked 值预分配给我的 new 操作。现在我有条件地渲染两个部分。一个部分包含带有 checked 属性的单选按钮,而另一个部分完全没有属性:
<%= form_for([@restaurant, @dish_review], url: :restaurant_dish_reviews) do |f| %>
<% if action_name == "new" %>
<%= render "status_buttons_checked", f: f, dish: @dish %>
<% else %>
<%= render "status_buttons", f: f %>
<% end %>
<% end %>
_status_buttons_checked
<div class="field">
<%= f.radio_button :status, :upvoted, checked: current_user.voted_up_on?(dish) %>
<%= f.label :status, value: :upvoted %>
<%= f.radio_button :status, :downvoted, checked: current_user.voted_down_on?(dish) %>
<%= f.label :status, value: :downvoted %>
</div>
_statsus_buttons
<div class="field">
<%= f.radio_button :status, :upvoted, checked: current_user.voted_up_on?(dish) %>
<%= f.label :status, value: :upvoted %>
<%= f.radio_button :status, :downvoted, checked: current_user.voted_down_on?(dish) %>
<%= f.label :status, value: :downvoted %>
</div>
我想知道 Rails 中是否有任何方法可以在 radio_button 参数中插入条件而不是创建两个部分。我想要类似于下面显示的内容,但遇到模板错误:
<%= f.radio_button :status, :downvoted, if action_name == "new" current_user.voted_down_on?(dish) %>
【问题讨论】:
标签: html ruby-on-rails parameters radio-button conditional