我正在建立一個stackoverflow克隆,並試圖實現問題和答案創建多個文件上傳。貝茨'nested_form嵌套資源沒有正確命名新字段
我使用carrierwave和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文件」並出現另一個文件中的字段,它正好被命名
name="question[attachments_attributes][1429430703012][file]"
因而在PARAMS我得到第二文件與問題相關,而不是答案。而且由於請求正在由answers_controller處理,所以第二個文件被忽略。
我如何才能使nested_form_for正確地命名新字段?
UPD
所以我一直在努力,明確說明父對象爲這樣的附件:
= f.fields_for :attachments, @answer.attachments do |field|
仍然沒有運氣