我目前有一部分可以通过以下单选按钮管理角色:
<%= simple_form_for user, :url => user_path(user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
<h3>Change Role</h3>
<%= f.input :role_ids, :collection => Role.all, :as => :radio_buttons, :label_method => lambda {|t| t.name.titleize}, :label => false, :item_wrapper_class => 'inline', checked: user.role_ids %>
<%= f.submit "Add or Remove Role", :class => "btn" %>
<% end %>
我想更改此内容,以便我可以使用复选框添加或删除多个角色。
<%= simple_form_for user, :url => user_path(user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
<h3>Change Role</h3>
<%= f.input :role_ids, :collection => Role.all, :as => :check_boxes, :label_method => lambda {|t| t.name.titleize}, :label => false, :item_wrapper_class => 'inline', checked: user.role_ids %>
<%= f.submit "Add or Remove Role", :class => "btn" %>
<% end %>
但这似乎不起作用,这是使用单选按钮时的控制台响应:
{"utf8"=>"✓", "authenticity_token"=>"MTV5MNomlkV86ynh0SVR6OW5SG3+9BCznWqcfq2xaWM=", "user"=>{"role_ids"=>"2"}, "commit"=>"Add or Remove Role", "id"=>"4"}
使用复选框时:
{"utf8"=>"✓", "authenticity_token"=>"MTV5MNomlkV86ynh0SVR6OW5SG3+9BCznWqcfq2xaWM=", "user"=>{"role_ids"=>["2", "3", "7", ""]}, "commit"=>"Add or Remove Role", "id"=>"2"}
那为什么不保存到数据库呢?为什么我最后在阵列中有空位?