我已經看到這個問題的其他問題,但到目前爲止答案沒有爲我工作。我正在嘗試使用註冊用戶的表單,並同時創建一個組織。用戶和組織通過分配表關聯。「不能批量分配受保護的屬性」嵌套屬性
這裏是我的形式:
= 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
謝謝,我有同樣的問題 – 2012-07-20 06:14:10