带复选框的嵌套对象 - 即使使用 Accept_nested_attributes_for 也可以进行批量分配?
我认为应该有一个简单的解决方案来解决这个问题,因为 Rails 2.3 具有这种新奇的嵌套表单功能。 基本上我想创建或更新用户并同时为他们分配角色。
看起来我做的一切都是正确的,但我收到错误警告:无法批量分配这些受保护的属性:roles_attrributes。
我什至尝试将视图更改为 user[permissions_attrributes][role_id] 因为我认为连接表可能会让 Rails 感到困惑。
无论如何,关于这实际上应该如何运作有什么建议吗?
视图中的模型
class User < ActiveRecord::Base
has_many :permissions
has_many :roles, :through => :permissions
accepts_nested_attributes_for :roles
accepts_nested_attributes_for :permissions
end
摘录(请注意,我尝试但未能让 fields_for 生成我想要的内容,也许这就是我的问题?)
<% for role in Role.all %>
<%= check_box_tag( "user[roles_attrributes][id]",role.id) %>
<%= role.rolename %>
<br/>
<% end %>
遇到的参数似乎是正确的:
{"user"=>{"password_confirmation"=>"[FILTERED]",
"roles_attrributes"=>{"id"=>"2"}, ...
解决方案 我拼写错误的组合,而不是使用attr_accessible,需要访问permissions_attributes,并且形式稍有偏差。
型号:
has_many :permissions, :dependent => :destroy
has_many :roles, :through => :permissions
accepts_nested_attributes_for :permissions
attr_accessible :permissions_attributes
视图:
<% Role.all(:order => "rolename ASC").each_with_index do |role,idx| %>
<%= check_box_tag( "user[permissions_attributes][#{idx}][role_id]",role.id) %>
<%= role.rolename %>
<br/>
<% end %>
I thought that there should have been a simple solution to this, given that Rails 2.3 has this newfangled nested forms feature. Basically I want to create or update a user and assign them roles at the same time.
It seems like I'm doing everything right but I get the error WARNING: Can't mass-assign these protected attributes: roles_attrributes.
I even tried changing the view to user[permissions_attrributes][role_id] because I thought that maybe the join table was confusing Rails.
Anyways, any suggestions on how this should actually work?
Model
class User < ActiveRecord::Base
has_many :permissions
has_many :roles, :through => :permissions
accepts_nested_attributes_for :roles
accepts_nested_attributes_for :permissions
end
Excerpt from view (notice I tried and failed to get fields_for to generate what I want here, maybe that's my problem?)
<% for role in Role.all %>
<%= check_box_tag( "user[roles_attrributes][id]",role.id) %>
<%= role.rolename %>
<br/>
<% end %>
Params coming across seem to be right:
{"user"=>{"password_confirmation"=>"[FILTERED]",
"roles_attrributes"=>{"id"=>"2"}, ...
Solution A combination of me misspelling, not using attr_accessible, needing to access permissions_attributes, and the form being slightly off.
Model:
has_many :permissions, :dependent => :destroy
has_many :roles, :through => :permissions
accepts_nested_attributes_for :permissions
attr_accessible :permissions_attributes
View:
<% Role.all(:order => "rolename ASC").each_with_index do |role,idx| %>
<%= check_box_tag( "user[permissions_attributes][#{idx}][role_id]",role.id) %>
<%= role.rolename %>
<br/>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您更正
check_box_tag
中属性的拼写,它看起来应该可以工作。If you correct the spelling of attributes in your
check_box_tag
, it looks like it should work.听起来这个属性没有被标记为可以安全更新。 您应该能够通过将以下内容添加到您的模型类中来修复它:
或者可能:
如果您查看,您可能已经有一个 attr_accessible 调用,您可以将其添加到其中。 有关详细信息,请参阅此处:
http://api.rubyonrails.org/类/ActiveRecord/Base.html#M002226
it sounds like this attribute isn't marked as safe for updating. You should be able to fix it by adding the following to your model class:
or possibly:
If you look, you may already have an attr_accessible call you can add this to. For more information this is documented here:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002226