2016-07-09 7 views
0

내 업데이트 기능은 제출시 모델의 중첩 항목에 대한 레코드를 두 배로 늘립니다.레일즈가 update_attributes의 레코드를 두 배로 만듭니다.

fields_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

을 이해할 수있을 것이다. without : id 중첩 된 매개 변수에 대한 각각의 업데이트는 새로운 레코드로 간주됩니다. 그것은 당신의 colors_attributes에 대해 다음과 같이해야합니다 : 당신은 또한이 방법에서 다른 중첩 된 속성을 수정해야

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

것 같아요.

관련 문제