This project is archived and is in readonly mode.

#327 ✓resolved
Kevin Glowacz

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

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>

Referenced by

Pages