我认为这是一个多选复选框
模型
class User < ActiveRecord::Base
has_many :user_roles, :dependent => :destroy
accepts_nested_attributes_for :user_roles, :allow_destroy => true
has_many :roles, :through => :user_roles
end
看法
<%= check_box_tag 'user[role_ids][]', role.id, user.blank? ? nil : user.roles.include?(role) ,id: dom_id(role)%>
为此的强参数写为
def user
params.require(:user).permit(:first_name,{:role_ids => []})
end
但是在创建时它说
Processing by Admin::UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+y8iWya5KIILqS0embEUEZuClycXq0O9Q4pA+MnbM0g=", "user"=>{"first_name"=>"", "last_name"=>"", "email"=>"a@loclahost.com", "language"=>"en", "access_level_id"=>"1", "role_ids"=>["", "1", "", "5", "", "", ""], "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create user"}
Unpermitted parameters: role_ids
Unpermitted parameters: role_ids
Unpermitted parameters: role_ids
Unpermitted parameters: role_ids
任何线索为什么它不接受来自表单的 role_ids 数组?