Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

nested fields_for with index should account for the index

#327

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

Reported by Kevin Glowacz · June 4th, 2008 @ 01:19 AM

State: resolved
Milestone: none
Assigned to: josh josh
Importance: none

Activity

  1. Kevin Glowacz
    Kevin Glowacz

    new patch with modified regex that works a lil better for me.

    June 5th, 2008 @ 04:49 PM

  2. Kevin Glowacz
    Kevin Glowacz
    • Tag set to actionpack, patch, tested

    updating tests to work with rails edge and the _erbout to output_buffer change

    June 30th, 2008 @ 08:17 PM

  3. Kevin Glowacz
  4. Kevin Glowacz
    Kevin Glowacz

    the updated patch for real this time

    June 30th, 2008 @ 08:35 PM

  5. RSL
    RSL

    Kevin, how does this handle nested fields_for with indexes on each fields_for? Don't have time to test myself.

    July 14th, 2008 @ 04:24 PM

  6. RSL
    RSL

    I do like this though. Just making sure it doesn't clash now that our other patch was committed.

    July 14th, 2008 @ 04:27 PM

  7. Kevin Glowacz
    Kevin Glowacz

    This 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
    end
    

    produces

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

    July 14th, 2008 @ 04:42 PM

  8. RSL
    RSL

    Was that in the test coverage? I musta missed in reading quickly.

    July 14th, 2008 @ 04:47 PM

  9. Kevin Glowacz
    Kevin Glowacz

    no 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.

    July 14th, 2008 @ 04:52 PM

  10. RSL
    RSL

    I guess I'm used to assuming nothing works without a test. [That wasn't meant to sound as TDDier-than-thou as it might].

    July 14th, 2008 @ 04:54 PM

  11. Kevin Glowacz
    Kevin Glowacz

    well 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.

    July 14th, 2008 @ 05:08 PM

  12. Kevin Glowacz
  13. Kevin Glowacz
    Kevin Glowacz

    It 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.

    July 14th, 2008 @ 05:53 PM

  14. Kevin Glowacz
    Kevin Glowacz

    updating 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.

    July 14th, 2008 @ 08:04 PM

  15. RSL
  16. Pratik
    Pratik
    • Assigned user set to josh

    July 19th, 2008 @ 04:35 PM

  17. Repository
    Repository
    • 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

    http://github.com/rails/rails/co...

    July 19th, 2008 @ 09:07 PM

  18. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 9th, 2010 @ 10:10 PM

  19. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 21st, 2010 @ 03:37 AM

  20. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    November 8th, 2010 @ 01:52 AM

  21. bingbing