Uploading multiple files to a Ruby on Rails app: first file doesn't get uploaded
For my Ruby on Rails application, I need the possibility to create several LectureNote objects at a time. I tried to do it with a view like this:
<% form_for @lecture_note, :html => {:multipart => true} do %>
<%= hidden_field_tag 'thematic_block_id', params[:thematic_block_id] %>
<table>
<% (1..8).each do |i| %>
<tr>
<td><%= file_field "new_lecture_notes[][file]", 'datafile', :accept => 'application/pdf' %></td>
<td><%= text_field_tag "new_lecture_notes[][pages_per_sheet]", '', :size => 8 %></td>
<td><%= check_box_tag "new_lecture_notes[][color]", "1" %></td>
<td><%= select_da开发者_运维百科te LectureNote.default_lecture_date, :order => [:day, :month, :year], :prefix => "new_lecture_notes[][lecture_date]" %></td>
</tr>
<% end %>
</table>
<%= submit_tag "Speichern" %>
<% end %>
However, the first file uploaded never appeared in the params hash, the second file appears at the place where the first should be, and so on. If I put the text_field_tag before the file_field, it works, though. Any ideas why?
I had a look at the Advanced Rails Recipes book now and in their Multiple Models in One Form example they use the fields_for and the text_field methods instead of text_field_tag, etc. Doing so made everything work for me, though I'm still not sure why the above form didn't work.
精彩评论