2012-05-16 24 views
0

尝试保存使用'accepched_nested_attributes_for'的表单时,出现'Can not mass-assign protected attribute'错误。我想我已经正确地编写了模型,但不知道我错过了什么。任何想法?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 

回答

相关问题