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

4

1 回答 1

9

您必须:organizations_attributesattr_accessible模型User中添加。

于 2012-07-10T00:35:54.870 回答