0

尝试保存使用“accepts_nested_attributes_for”的表单时出现“无法批量分配受保护的属性”错误。我想我已经正确地对模型进行了编码,但不确定我错过了什么。任何的想法?

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

用户.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

组织.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
4

1 回答 1

0

根据这个问题的答案设法得到我想要的东西:Use both Account and User tables with Devise

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

于 2012-05-18T02:11:32.850 回答