Rails 2: use form_for to build a form covering multiple objects of the same class

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" />

<strong>However</strong> 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.

    <li>... 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?</li> </ul>


    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.


    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.


  • Can't create new post
  • Rails 3 UJS dry Client + Server Side form validation
  • Rails ajax form not submitting from within another
  • undefined method `wikis_path'
  • Rails how to validate file format?
  • undefined method `posts_path' for #
  • I can't create model objects using accepts_nested_attributes_for. It won't create the nest
  • How to Add Polymorphic Comments to Feed?
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • Connect .sks to skscene.h
  • Debug.DrawLine not showing in the GameView
  • GridView breaks while scrolling
  • Jackson Parser: ignore deserializing for type mismatch
  • How to Cache Real-time Data?
  • How to use RequestBodyAdvice
  • Record samples being played with OpenAL
  • Breeze - Deleted Items nav properties bug
  • Q promise. Difference between .when and .then
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • javaw.exe and eclipse startup problems
  • Align navbar back button on right side
  • Can Jackson SerializationFeature be overridden per field or class?
  • Incrementing object id automatically JS constructor (static method and variable)
  • Does CUDA 5 support STL or THRUST inside the device code?
  • When should I choose bucket sort over other sorting algorithms?
  • Hazelcast - OperationTimeoutException
  • To display the title for the current loaction in map in iphone
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How do I configure my settings file to work with unit tests?
  • How to stop GridView from loading again when I press back button?
  • need help with bizarre java.net.HttpURLConnection behavior
  • Is it possible to post an object from jquery to bottle.py?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • How to push additional view controllers onto NavigationController but keep the TabBar?