嵌套表单未正确更新文件字段

时间:2015-07-28 06:52:22

标签: ruby-on-rails ruby nested-forms nested-form-for

我有一个嵌套表单,其中的字段包含User,Profile和Professional Details Model。用户有一个个人资料。配置文件有许多专业详细信息(包含文档作为文件字段和描述文本字段)。

我在这里遇到的问题是在更新我的个人资料页面时,该页面是这3个模型的嵌套形式。当我添加新的专业细节并提交表格时,旧的专业细节将变为零,并且专业细节的新对象将被创建。参数就像这样

 {"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"},

这是旧的专业细节对象。更新用户后,文档部分为零。     为什么以前存储的Document在这种情况下会得到nil?

1 个答案:

答案 0 :(得分:0)

正如我所说,在:id方法中添加strong_params可以解决您的问题

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]])