2016-07-09 6 views
0

私の更新機能は、サブミット時にモデル内のネストされたアイテムのレコードを2倍にします。Railsはupdate_attributesのレコードを2倍にします。

fields_forには含まれていませんが、expect_forとして機能しますが、fields_forのすべてのレコードは倍増しています。

私には何が欠けていますか? IDをprinter_paramsに:すべてのヘルプは非常にあなたが欠落している

def edit 
    @printer = Printer.find(params[:id]) 
    end 

    def update 
    @printer = Printer.find(params[:id]) 
    if @printer.update_attributes(printer_params) 
     redirect_to @printer 
    else 
     render 'edit' 
    end 
    end 

def printer_params 
    params.require(:printer).permit(:model, colors_attributes: [:color], materials_attributes: [:material], printer_photos_attributes: [:image_url]) 
end 

edit.html.erb

<%= form_for @printer, html: { multipart: true }, :url => url_for(:controller => 'printers', :action => 'update') do |p|%> 

     <%= p.text_field :model %> 

     <%= p.fields_for :colors do |color|%> 
      <%= color.text_field :color %> 
     <% end %> 

     <%= p.submit "Edit" %> 
    <% end %> 
+1

[この質問](http://stackoverflow.com/questions/9944065)とその回答を確認しましたか? –

答えて

1

を理解されるであろう。なし:idでは、ネストされたパラメータの更新はすべて新しいレコードとみなされます。それはあなたのcolors_attributesために、次のようにする必要があります:

def printer_params 
    params.require(:printer).permit(:model, colors_attributes: [:id, :color], materials_attributes: [:material], printer_photos_attributes: [:image_url]) 
end 

私は推測、あなたも、この方法では、あなたの他のネストされた属性を修正する必要があります。

関連する問題