“不能批量分配受保护的属性”嵌套属性

问题描述:

我已经看到这个问题的其他问题,但到目前为止答案没有为我工作。我正在尝试使用注册用户的表单,并同时创建一个组织。用户和组织通过分配表关联。“不能批量分配受保护的属性”嵌套属性

这里是我的形式:

= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| 

    = devise_error_messages! 

    = f.fields_for :organizations do |f| 

    = f.label :name 
    = f.text_field :name 

    = f.label :email 
    = f.email_field :email 

    = f.label :password 
    = f.password_field :password 

    = f.label :password_confirmation 
    = f.password_field :password_confirmation 

我登记控制器:

class Users::RegistrationsController < Devise::RegistrationsController 
    def new 
    @user = User.new 
    @user.organizations.build 
    end 

    def create 
    super 
    end 

    def update 
    super 
    end 
end 

我的组织模式:

class Organization < ActiveRecord::Base 
    has_many :organization_assignments 
    has_many :users, :through => :organization_assignments 

    attr_accessible :name 
end 

和我的用户模型:

class User < ActiveRecord::Base 

    has_many :organization_assignments 
    has_many :organizations, :through => :organization_assignments 

    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, 
    # :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    accepts_nested_attributes_for :organizations 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :organization_attributes 
    # attr_accessible :title, :body 

end 

我得到确切的错误是:

无法大规模指派保护的属性:organizations_attributes

+0

谢谢,我有同样的问题 – 2012-07-20 06:14:10

你必须在User模型添加:organizations_attributesattr_accessible

+0

谢谢!最后作品 – Asherlc 2012-07-10 01:56:12

+1

它也适用于我 – 2012-07-20 06:13:54

+0

是否与他已经在用户模型中的复数:organizational_attributes而不是单数:organization_attributes之间的区别?我得到了同样的错误信息,但是这并没有解决它。 – Mittenchops 2012-08-10 19:31:16