2

This is hopefully a slam dunk for a Rails maven :

I have a form that is supposed to iterate over a set of LineItems and gather information on each. I am trying to get form_for and fields_for to create the proper field names and parameters to give my controller a params hash something like

params[:line_items] = { <line_item.id> => { :rma_reason => "some string", :rma_qty => 2 } }

What I have is:

<% form_for(@object) do |rma_form| %>
  <% @order.line_items.each do |item| %>
  ....
    <% fields_for item do |item_f| %>
      <%= item_f.text_area :rma_reason %>
      <%= item_f.text_field :rma_qty, :value => item.quantity, :size=>3 %>
    <% end %>
  ....
  <% end %>
<% end %>

And the resultant HTML looks like (what you would expect, I guess):

<textarea id="line_item_id" name="line_item[id]"></textarea> 
<input id="line_item_rma_qty" name="line_item[rma_qty]" size="3" type="text" value="1" />

However what I am trying to get (and failing to find any solution beyond going back to ad-hoc HTML tags) is, field names that are indexed by the line_item.id, e.g. line_item[1928][rma_qty] for instance. This would allow my controller to iterate over each line_item and update it with the given params.

  • ... maybe in a pure REST UI, the form should be tied to ONE instance of LineItem, and the standard form_for would work - but the user should be able to submit all the changes at once with one form. Is it possible?
sbeam
  • 4,622
  • 6
  • 33
  • 43

2 Answers2

1

When you're nesting form fields, you have to make sure you are chaining the fields_for methods form the appropriate form builder:

<% rma_form.fields_for :line_item, item do |item_f| %>

by calling fields_for alone, you were breaking the chain from form_for and thus the incorrect field names.

Adam Lassek
  • 35,156
  • 14
  • 91
  • 107
  • that didn't get me the line_item.id in the params, but you were right and it get me going down the road of figuring out how fields_for actually works - though it still seems very strange sometimes - thanks! – sbeam Nov 18 '10 at 21:23
  • @sbeam my bad, I was missing the first param for `fields_for`. I think using symbols for model refs in these helpers is slightly more readable. – Adam Lassek Nov 19 '10 at 01:39
0

the fields_for needs to be called on the main form helper, and a string parameter with magic []s and an argument indicating which instance should be inserted in the []s

<% rma_form.fields_for "line_item[]", item do |item_f| %>

so now my tag looks something like

<input id="return_authorization_line_item_1070870205_rma_qty" name="return_authorization[line_item][1070870205][rma_qty]" size="3" type="text" value="1" />

which is peachy.

this classic railscast is a good fields_for intro, and this comment explains the effect of the magic bracket that did what I wanted - other than that, couldn't find this feature documented anywhere.

sbeam
  • 4,622
  • 6
  • 33
  • 43