嵌套的对象W /复选框 - 大众分配甚至有accepts_nested_attributes_for? [英] Nested Object w/ Checkboxes - mass-assignment even with accepts_nested_attributes_for?
问题描述
我觉得应该有一个简单的解决办法,考虑到Rails的2.3有这个新奇的嵌套形式的功能。基本上我要创建或更新一个用户,并在同一时间将它们分配角色。
好像我做的一切权利,但我得到的错误警告:不能批量分配这些保护属性:roles_attrributes
我甚至试图改变以用户[permissions_attrributes] [ROLE_ID],因为我想,也许联接表是令人困惑的Rails。
不管怎么说,就如何实现这一实际工作有什么建议?
型号
类用户的LT;的ActiveRecord :: Base的
的has_many:权限
的has_many:角色:通过=> :权限
accepts_nested_attributes_for:角色
accepts_nested_attributes_for:权限
结束
从视图中摘录(请注意,我尝试和失败让fields_for产生什么,我想在这里,也许这是我的问题吗?)
<%在Role.all%GT的作用;
<%= check_box_tag(用户[roles_attrributes] [ID],role.id)%>
&其中;%= role.rolename%GT;
< BR />
<%结束%GT;
PARAMS来到对面似乎是正确的:
{用户=> {password_confirmation=>中[FILTERED],
roles_attrributes=> {ID=>中2},...
解决方案我的组合拼写错误,不使用attr_accessible,需要访问permissions_attributes和形式是稍微偏离。
型号:
的has_many:权限:依赖=> :破坏
的has_many:角色:通过=> :权限
accepts_nested_attributes_for:权限
attr_accessible:permissions_attributes
查看:
<%Role.all(:为了=>中角色名ASC)。each_with_index办|作用,IDX | %>
<%= check_box_tag(用户[permissions_attributes] [#{IDX}] [ROLE_ID],role.id)%>
&其中;%= role.rolename%GT;
< BR />
<%结束%GT;
这听起来像这样的属性不标记为可安全更新。您应该能够解决它通过添加以下到你的模型类:
attr_accessible:角色
或可能是:
attr_accessible:roles_attributes
如果你看,你可能已经有一个attr_accessible呼叫时,可以将它添加到。欲了解更多信息,这是记录在这里:
http://api.rubyonrails.org/classes/ActiveRecord/Base。 HTML#M002226
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 %>
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:
attr_accessible :roles
or possibly:
attr_accessible :roles_attributes
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
这篇关于嵌套的对象W /复选框 - 大众分配甚至有accepts_nested_attributes_for?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!