1
我有一對單選按鈕,我想預先將checked
值分配給我的new
操作。現在我有條件地呈現兩個部分。一個部分具有單選按鈕與checked
屬性和其他與沒有屬性可言:有條件地在Rails中設置html參數
<%= 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) %>