2013-04-20 45 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|