“无法使用嵌套属性批量分配受保护的属性”

时间:2012-07-10 00:23:50

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 devise

我已经看到了这个问题的其他问题,但到目前为止,答案对我没有用。我正在尝试使用一个注册用户的表单,并同时创建一个组织。用户和组织通过分配表关联。

这是我的表格:

= 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

1 个答案:

答案 0 :(得分:9)

您必须在:organizations_attributes模型中将attr_accessible添加到User