現在、stackoverflow のクローンを作成しており、質問と回答の作成時に複数のファイルのアップロードを実装しようとしています。
私は、nested_form gem で Carrierwave を使用しています。
質問作成時のファイル アップロードは問題なく機能しますが、回答に対して機能させることはできません。新しい回答のフォームは次のとおりです。
= 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 では、答えではなく、質問に関連付けられた 2 番目のファイルを取得します。そして、リクエストはanswers_controllerによって処理されているため、2番目のファイルは無視されています.
nested_form_for という名前の新しいフィールドを適切に作成するにはどうすればよいですか?
UPD
だから私は、このような添付ファイルの親オブジェクトを明示的に述べようとしました:
= f.fields_for :attachments, @answer.attachments do |field|
まだ運がない