Rails: How do I make a form submit an array of records?
I need to make a form that will submit an array of records. I am using rails 2.3.11 and trying to follow the instructions in the one to many section here: http://api.rubyonrails.org/v2.3.8/classes/ActionView/Helpers/FormHelper.html
However when I do it, I get errors, or the form only submits the last record in the array.
This doesn't work, because @query[:filters] is an array,开发者_如何学C so fields_for will throw an exception:
<% form_for :aquery, @query, :url => {:action => 'index'} do |a| %>
<% a.fields_for :filters, @query[:filters] do |f| %>
<p>
<%= f.select :field, @fields %>
<%= f.select :logic, ["all", "is", "is not", "none"]%>
<%= f.text_field :arg, :size => 30 %>
</p>
<% end %>
<%= submit_tag l(:button_apply) %>
<% end %>
This doesn't work because it ignores the contents of @query[:filters], and only renders one set of fields regardless of how large the array is:
<% a.fields_for :filters do |f| %>
And this doesn't work because it will only submit the last item in the array:
<% form_for :aquery, @query, :url => {:action => 'index'} do |a| %>
<% @query[:filters].each do |filter| %>
<% a.fields_for :filters, filter do |f| %>
<p>
<%= f.select :field, @fields %>
<%= f.select :logic, ["all", "is", "is not", "none"]%>
<%= f.text_field :arg, :size => 30 %>
</p>
<% end %>
<% end %>
<%= submit_tag l(:button_apply) %>
<% end %>
You need to run over your filters and do the indexing manually.
<% form_for :aquery, @query, :url => {:action => 'index'} do |a| %>
<% @query[:filters].each_with_index do |filter, i| %>
<% a.fields_for :filters, filter, :index => i do |f| %>
<p>
<%= f.select :field, @fields %>
<%= f.select :logic, ["all", "is", "is not", "none"]%>
<%= f.text_field :arg, :size => 30 %>
</p>
<% end %>
<% end %>
<%= submit_tag l(:button_apply) %>
<% end %>
精彩评论