accepts_nested_attributes_for给出错误:无法批量分配受保护的属性

时间:2012-05-16 08:02:00

标签: ruby-on-rails

在尝试保存使用“accepts_nested_attributes_for”的表单时,我收到“无法大量分配受保护的属性”错误。我想我已正确编码模型,但不确定我错过了什么。有什么想法吗?

错误:无法批量分配受保护的属性:组织

user.rb

class User < ActiveRecord::Base
  belongs_to :organization

  accepts_nested_attributes_for :organization

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me,
                  :username, :first_name, :last_name, :organization_attributes
end

organization.rb

class Organization < ActiveRecord::Base
  has_many :users

  attr_accessible :address1, :address2, :city, :country, :fax, :name, :phone, :state, :zip
end

users_controller.rb

  def new
    @user = User.new
    @organization = Organization.new

    respond_to do |format|
      format.html # new.html.erb
    end
  end

  def create
    @user = User.new(params[:user])
    @organization = @user.organizations.build(params[:organization])

    respond_to do |format|
      if @user.save
        @organization.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
      else
        format.html { render action: "new" }
      end
    end
  end

1 个答案:

答案 0 :(得分:0)

根据此问题的答案进行管理以获得我想要的内容:Use both Account and User tables with Devise

顺便说一句,我也在使用STI,效果很好。