2012-07-10 150 views
4

我已經看到這個問題的其他問題,但到目前爲止答案沒有爲我工作。我正在嘗試使用註冊用戶的表單,並同時創建一個組織。用戶和組織通過分配表關聯。「不能批量分配受保護的屬性」嵌套屬性

這裏是我的形式:

= 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

回答

9

你必須在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

相關問題