2013-04-20 71 views
0

我已经完成了这10次,每次我似乎遇到嵌套窗体的一些问题。下面是我有:嵌套形式的另一个质量分配错误

客户机控制器:

def new 
    @client = Client.new 
    @contact = @client.contacts.new 
    @header = "New Client" 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @client } 
    end 
    end 

客户端类:

class Client < ActiveRecord::Base 
    ## ASSOCIATIONS ## 
    belongs_to :user 
    has_many :contacts, :dependent => :destroy 
    has_many :invoices 

    ## ACCESSIBLE ## 
    attr_accessible :name, :address_line_one, :address_line_two, 
         :contacts_attributes 
    ## NESTED ATTRIBUTES ## 
    accepts_nested_attributes_for :contacts 
形式

= form_for(@client) do |f| 
    = f.fields_for(@contact) do |contact| 

,但在提交表单的时候我仍然得到这个错误:

Can't mass-assign protected attributes: contact 

和PARAMS:

"client"=>{"name"=>"23", 
"contact"=>{"name"=>"asdf", 
"email"=>"[email protected]"}}, 
"commit"=>"Save"} 
+0

您是否尝试过:contact_attributes和accepts_nested_attributes_for:联系?我通常会发现它的命名约定问题,或者有一个与模型名称相同的db列。 – dodgerogers747 2013-04-20 17:51:17

+0

我不认为这是它,因为它是一对多的关系。 – 2013-04-20 17:53:26

回答

0

我会做的是:

def new 
    @client = Client.new 
    @client.contacts.build 
    @header = "New Client" 

    respond_to do |format| 
    format.html # new.html.erb 
    format.json { render json: @client } 
    end 
end 

和:

= form_for(@client) do |f| 
    = f.fields_for(:contacts) do |contact|