Search code examples
ruby-on-railsrubynested-formsnested-form-for

Nested Form Not Updating File Fields Properly


I have a nested form with fields containing of User,Profile and Professional Details Model. User has one Profile. Profile has Many Professional Details (containing a document as file field and a description text field).

The Issue I am facing here is while updating my profile page which is nested form of these 3 models. When I add new professional details and submit the form old professional details get nil and new object for the professional details get created. Params come like this

 {"utf8"=>"✓",     "_method"=>"patch","authenticity_token"=>"+pPcQC1hVJ+u0NS+ApOJbfwGG4NCBkURUA+BZ4ZaTMpMKQihrymC7aVl59D89SnGxt08kCMItqph2yXkOp1WsQ==",
 "user"=>
  {"dob"=>"1944-06-07",
   "name"=>"Etikazzz",
   "tag_list"=>"",
   "profile_attributes"=>
    {"gmc_number"=>"",
     "phone_number"=>"919999999999",
     "description"=>"hellloo",
     "job_description"=>"Engineer1",
     "professional_details_attributes"=>
      {"0"=>{"description"=>"fg", "id"=>"139"},
       "1"=>
        {"document"=>
          #<ActionDispatch::Http::UploadedFile:0xbdd3d160
           @content_type="application/pdf",
           @headers=
            "Content-Disposition: form-data; name=\"user[profile_attributes][professional_details_attributes][1][document]\"; filename=\"pdf-sample.pdf\"\r\nContent-Type: application/pdf\r\n",
           @original_filename="pdf-sample.pdf",
           @tempfile=#<File:/tmp/RackMultipart20150722-2984-onumi8.pdf>>,
         "description"=>"sd"}},
     "id"=>"74"}},
 "commit"=>"Complete Profile",
 "controller"=>"profiles",
 "action"=>"complete_profile"}    
"0"=>{"description"=>"fg", "id"=>"139"},

this is the old professional detail object. The document part gets nil after updating the User. Why does the Document of the previously stored get nil in this case?


Solution

  • As I said, adding an :id to your strong_params method should solve your problem

    params.require(:user).permit(:id,:tag_list,:email,:role_ids, :activation_token,:uid,:provider,:password,:password_confirmation,:name,:avatar,‌​:dob, profile_attributes: [:id,:address,:country_id,:state_id,:city,:gmc_number,:phone_number,:description‌​,:job_description,professional_details_attributes:[:document,:id,:description,:_d‌​estroy]])