“无法质量分配受保护的属性”具有嵌套属性 [英] "Can't mass-assign protected attributes" with nested attributes
本文介绍了“无法质量分配受保护的属性”具有嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到有其他问题的问题,但到目前为止,答案没有为我工作。我正在尝试注册一个用户的表单,同时创建一个组织。用户和组织通过分配表相关联。
I've seen other questions with this problem, but so far the answers haven't worked for me. I'm trying to have a form that registers a User, and creates an Organization at the same time. The User and Organization are associated via an assignment table.
这是我的表单:
= 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
我得到的确切错误是:
无法大量分配受保护的属性:organizational_attributes
Can't mass-assign protected attributes: organizations_attributes
推荐答案
你必须添加:organizational_attributes
到 attr_accessible
在用户
模型中。
You have to add :organizations_attributes
to attr_accessible
in the User
model.
这篇关于“无法质量分配受保护的属性”具有嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文