This project is archived and is in readonly mode.

nested fields_for with index should account for the index
Reported by Kevin Glowacz | June 4th, 2008 @ 01:19 AM
Currently, when you nest fields_for, the index or auto index on the outer fields_for is not accounted for when creating the ids and names for the fields in the inner fields_for.
Here are the tests for what this implements:
  def test_nested_fields_for_with_index
    _erbout = ''
    form_for(:post, @post, :index => 1) do |f|
      f.fields_for(:comment, @post) do |c|
        _erbout.concat c.text_field(:title)
      end
    end
    expected = "<form action='http://www.example.com' method='post'>" +
               "<input name='post[1][comment][title]' size='30' type='text' id='post_1_comment_title' value='Hello World' />" +
               "</form>"
    assert_dom_equal expected, _erbout
  end
  def test_nested_fields_for_with_auto_index
    _erbout = ''
    form_for("post[]", @post) do |f|
      f.fields_for(:comment, @post) do |c|
        _erbout.concat c.text_field(:title)
      end
    end
    expected = "<form action='http://www.example.com' method='post'>" +
               "<input name='post[123][comment][title]' size='30' type='text' id='post_123_comment_title' value='Hello World' />" +
               "</form>"
    assert_dom_equal expected, _erbout
  end
Comments and changes to this ticket
- 
            
         Kevin Glowacz June 5th, 2008 @ 04:49 PMnew patch with modified regex that works a lil better for me. 
- 
            
         Kevin Glowacz June 30th, 2008 @ 08:17 PM- Tag set to actionpack, patch, tested
 updating tests to work with rails edge and the _erbout to output_buffer change 
- 
         Kevin Glowacz June 30th, 2008 @ 08:35 PM- no changes were found...
 
- 
            
         
- 
            
         RSL July 14th, 2008 @ 04:24 PMKevin, how does this handle nested fields_for with indexes on each fields_for? Don't have time to test myself. 
- 
            
         RSL July 14th, 2008 @ 04:27 PMI do like this though. Just making sure it doesn't clash now that our other patch was committed. 
- 
            
         Kevin Glowacz July 14th, 2008 @ 04:42 PMThis does work with indexes on the nested fields for. (It is actually what I use this for.) form_for(:post, @post, :index => 1) do |f| f.fields_for(:comment, @post, :index => 5) do |c| concat c.text_field(:title) end endproduces <form action="http://www.example.com" method="post"> <input name="post[1][comment][5][title]" size="30" type="text" id="post_1_comment_5_title" value="Hello World" /> </form>
- 
            
         
- 
            
         Kevin Glowacz July 14th, 2008 @ 04:52 PMno it isn't in the patch. I just assumed it would be assumed that everything else continued to work. I suppose I should add it in. 
- 
            
         RSL July 14th, 2008 @ 04:54 PMI guess I'm used to assuming nothing works without a test. [That wasn't meant to sound as TDDier-than-thou as it might]. 
- 
            
         Kevin Glowacz July 14th, 2008 @ 05:08 PMwell those tests only showed the exact behaviour that changed, but no one ever complains about test being to explicit. More tests are on the way. 
- 
            
         
- 
            
         Kevin Glowacz July 14th, 2008 @ 05:53 PMIt is a good thing you had me write those tests. I discovered that nested auto_index didn't work. Now it does along with more tests to cover that the inner fields_for having index and auto index work. 
- 
            
         Kevin Glowacz July 14th, 2008 @ 08:04 PMupdating patch to apply cleanly to edge. I think the only difference is in the hashes and line numbers, but I get an error with the old patch for some reason. And apparently I can't delete my old attachments anymore. 
- 
            
         
- 
         Pratik July 19th, 2008 @ 04:35 PM- Assigned user set to josh
 
- 
         Repository July 19th, 2008 @ 09:07 PM- State changed from new to resolved
 (from [1b4b1aa725a4f44c3473ae99b36d7cededba2bea]) Fixed index and auto index for nested fields_for [#327 state:resolved] Signed-off-by: Joshua Peek 
- 
         
- 
         
- 
         
- 
            
         
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 
         181 
          Incorrect input name with nested fields_for
        
There's been some changes to how index is handled as wel... 181 
          Incorrect input name with nested fields_for
        
There's been some changes to how index is handled as wel...
- 
         181 
          Incorrect input name with nested fields_for
        
#327 has patch 181 
          Incorrect input name with nested fields_for
        
#327 has patch
 Jeremy Kemper
      Jeremy Kemper
 josh
      josh
 Kevin Glowacz
      Kevin Glowacz
 Pratik
      Pratik
 Repository
      Repository
 RSL
      RSL
 Ryan Bigg
      Ryan Bigg