Bates 的 nested_form 嵌套资源未正确命名新字段
Bates' nested_form with nested resource doesn't name new fields properly
我现在正在构建一个 Whosebug 克隆,并尝试在创建问答时实现多个文件上传。
我正在使用 nested_form gem 的载波。
创建问题时的文件上传工作正常,但我无法让它用于答案。
这是我的新答案表格:
= nested_form_for [@question, @answer], remote: true do |f|
= render 'shared/error_messages', object: f.object
div.form-group
= f.label :body
= f.text_area :body, class: 'form-control'
div.form-group
= f.fields_for :attachments do |field|
= field.label :file
= field.file_field :file
= field.link_to_remove "Remove this attachment"
p
= f.link_to_add "Moar files!", :attachments
div.form-group
= f.submit 'Post answer', class: 'btn btn-primary'
当我尝试上传单个文件时它工作正常。嵌套的 file_field 的名称是这样的:
name="answer[attachments_attributes][0][file]"
但是当我点击 'Moar files' 并且出现另一个文件字段时,它恰好被命名为
name="question[attachments_attributes][1429430703012][file]"
因此在参数中我得到了与问题相关的第二个文件,而不是答案。由于请求由 answers_controller 处理,第二个文件被忽略。
如何正确创建 nested_form_for name 新字段?
UPD
所以我尝试像这样显式声明附件的父对象:
= f.fields_for :attachments, @answer.attachments do |field|
仍然没有运气
事实证明这是 nested_form 的一个老问题,当它在一个页面上多次用于具有相同多态关联的两个不同对象时。
这里data-blueprint-id conflict on Nested form for polymorphic associations是描述相同问题的另一个问题。
希望对大家有所帮助。
我现在正在构建一个 Whosebug 克隆,并尝试在创建问答时实现多个文件上传。
我正在使用 nested_form gem 的载波。
创建问题时的文件上传工作正常,但我无法让它用于答案。 这是我的新答案表格:
= nested_form_for [@question, @answer], remote: true do |f|
= render 'shared/error_messages', object: f.object
div.form-group
= f.label :body
= f.text_area :body, class: 'form-control'
div.form-group
= f.fields_for :attachments do |field|
= field.label :file
= field.file_field :file
= field.link_to_remove "Remove this attachment"
p
= f.link_to_add "Moar files!", :attachments
div.form-group
= f.submit 'Post answer', class: 'btn btn-primary'
当我尝试上传单个文件时它工作正常。嵌套的 file_field 的名称是这样的:
name="answer[attachments_attributes][0][file]"
但是当我点击 'Moar files' 并且出现另一个文件字段时,它恰好被命名为
name="question[attachments_attributes][1429430703012][file]"
因此在参数中我得到了与问题相关的第二个文件,而不是答案。由于请求由 answers_controller 处理,第二个文件被忽略。
如何正确创建 nested_form_for name 新字段?
UPD
所以我尝试像这样显式声明附件的父对象:
= f.fields_for :attachments, @answer.attachments do |field|
仍然没有运气
事实证明这是 nested_form 的一个老问题,当它在一个页面上多次用于具有相同多态关联的两个不同对象时。
这里data-blueprint-id conflict on Nested form for polymorphic associations是描述相同问题的另一个问题。 希望对大家有所帮助。