在Ruby on Rails中制作与某个问题相关的复选框的正确方法是什么?目前我有:
<%= check_box_tag 'features[]', 'scenarios' %> Scenarios
<%= check_box_tag 'features[]', 'role_profiles' %> Role profiles
<%= check_box_tag 'features[]', 'private_messages' %> Private messages
<%= check_box_tag 'features[]', 'chatrooms' %> Chatrooms
<%= check_box_tag 'features[]', 'forums' %> Forums
<%= check_box_tag 'features[]', 'news' %> News
<%= check_box_tag 'features[]', 'polls' %> Polls
我还希望能够自动检查以前选择的项目(如果重新加载此表单).如何将参数加载到这些的默认值?
您正在查看以下内容:
<% [ 'scenarios', 'role_profiles', ... , 'polls' ].each do |feature| %>
<%= check_box_tag 'features[]', feature, (params[:features] || {}).include?(feature) %> <%= feature.humanize %> <% end %>
虽然如果你已经拥有一个Feature
带有features
表和has_many :features
关系的模型,你可能想要这样:
<% for feature in Feature.find(:all) do %>
<%= check_box_tag 'feature_ids[]', feature.id, @model.feature_ids.include?(feature.id) %> <%= feature.name.humanize %> <% end %>