0
@@ -6,7 +6,7 @@ silence_warnings do
0
alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast)
0
alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
0
alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
0
- alias_method :secret?, :secret
0
+ alias_method :secret?, :secret
0
def new_record=(boolean)
0
@@ -22,6 +22,7 @@ silence_warnings do
0
def save; @id = 1; @post_id = 1 end
0
def new_record?; @id.nil? end
0
+ def to_param; @id; end
0
@id.nil? ? 'new comment' : "comment ##{@id}"
0
class Comment::Nested < Comment; end
0
class FormHelperTest < ActionView::TestCase
0
tests ActionView::Helpers::FormHelper
0
@@ -447,6 +447,117 @@ class FormHelperTest < ActionView::TestCase
0
assert_dom_equal expected, output_buffer
0
+ def test_nested_fields_for_with_nested_collections
0
+ form_for('post[]', @post) do |f|
0
+ concat f.text_field(:title)
0
+ f.fields_for('comment[]', @comment) do |c|
0
+ concat c.text_field(:name)
0
+ expected = "<form action='http://www.example.com' method='post'>" +
0
+ "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
0
+ "<input name='post[123][comment][][name]' size='30' type='text' id='post_123_comment__name' value='new comment' />" +
0
+ assert_dom_equal expected, output_buffer
0
+ def test_nested_fields_for_with_index
0
+ form_for('post', @post, :index => 1) do |c|
0
+ concat c.text_field(:title)
0
+ c.fields_for('comment', @comment, :index => 1) do |r|
0
+ concat r.text_field(:name)
0
+ expected = "<form action='http://www.example.com' method='post'>" +
0
+ "<input name='post[1][title]' size='30' type='text' id='post_1_title' value='Hello World' />" +
0
+ "<input name='post[1][comment][1][name]' size='30' type='text' id='post_1_comment_1_name' value='new comment' />" +
0
+ assert_dom_equal expected, output_buffer
0
+ def test_nested_fields_for_with_index
0
+ form_for(:post, @post, :index => 1) do |f|
0
+ f.fields_for(:comment, @post) do |c|
0
+ concat c.text_field(:title)
0
+ expected = "<form action='http://www.example.com' method='post'>" +
0
+ "<input name='post[1][comment][title]' size='30' type='text' id='post_1_comment_title' value='Hello World' />" +
0
+ assert_dom_equal expected, output_buffer
0
+ def test_nested_fields_for_with_index_on_both
0
+ form_for(:post, @post, :index => 1) do |f|
0
+ f.fields_for(:comment, @post, :index => 5) do |c|
0
+ concat c.text_field(:title)
0
+ expected = "<form action='http://www.example.com' method='post'>" +
0
+ "<input name='post[1][comment][5][title]' size='30' type='text' id='post_1_comment_5_title' value='Hello World' />" +
0
+ assert_dom_equal expected, output_buffer
0
+ def test_nested_fields_for_with_auto_index
0
+ form_for("post[]", @post) do |f|
0
+ f.fields_for(:comment, @post) do |c|
0
+ concat c.text_field(:title)
0
+ expected = "<form action='http://www.example.com' method='post'>" +
0
+ "<input name='post[123][comment][title]' size='30' type='text' id='post_123_comment_title' value='Hello World' />" +
0
+ assert_dom_equal expected, output_buffer
0
+ def test_nested_fields_for_with_auto_index_on_both
0
+ form_for("post[]", @post) do |f|
0
+ f.fields_for("comment[]", @post) do |c|
0
+ concat c.text_field(:title)
0
+ expected = "<form action='http://www.example.com' method='post'>" +
0
+ "<input name='post[123][comment][123][title]' size='30' type='text' id='post_123_comment_123_title' value='Hello World' />" +
0
+ assert_dom_equal expected, output_buffer
0
+ def test_nested_fields_for_with_index_and_auto_index
0
+ form_for("post[]", @post) do |f|
0
+ f.fields_for(:comment, @post, :index => 5) do |c|
0
+ concat c.text_field(:title)
0
+ form_for(:post, @post, :index => 1) do |f|
0
+ f.fields_for("comment[]", @post) do |c|
0
+ concat c.text_field(:title)
0
+ expected = "<form action='http://www.example.com' method='post'>" +
0
+ "<input name='post[123][comment][5][title]' size='30' type='text' id='post_123_comment_5_title' value='Hello World' />" +
0
+ "<form action='http://www.example.com' method='post'>" +
0
+ "<input name='post[1][comment][123][title]' size='30' type='text' id='post_1_comment_123_title' value='Hello World' />" +
0
+ assert_dom_equal expected, output_buffer
0
fields_for(:post, @post) do |f|
0
concat f.text_field(:title)
0
@@ -831,7 +942,6 @@ class FormHelperTest < ActionView::TestCase
0
assert_dom_equal expected, output_buffer
0
def comments_path(post)
0
"/posts/#{post.id}/comments"
I am attempting to utilize mass-assign for my models, but can’t get the update_attributes to work correctly when I have association_collections as child models. My create methods work fine, since the hash passes in an Array value for these collections. I.E.: emails => [ {...}, {...} ] ) But, when I try to edit/update my model, the field names in the form naturally have the :id of the email included in them, so that the resulting value passed to the mass-assign is not an Array, but a Hash of this form: emails => {“5” => {...}, “6” => {...}} Where the 5 and 6 are the :id values of the respective emails. This gets passed into the update_attributes, which eventually gets to: AssociationCollection.replace(other_array) which assumes “emails” to be an Array. Shouldn’t this replace method be “smarter” so that if a Hash is passed in, it will ascertain the :id values from it, and then reassign the model attributes accordingly? Furthermore, what would happen if there were a combination of updated models (emails) and perhaps one new email (that didn’t have an :id). Does the mass-assign functionality handle this situation? Any help would be appreciated. -Glenn
Generation of HTML element id and form field name are inconsistent after 2nd level of nesting.
Generates this:You notice that the in the 3rd level that the 2nd level ID is missing and that the “[]” are enclosed within the first field.
I was able to get the the above form to work as expected by removing the brackets and explicitly setting the id.Generation of HTML element id and form field name are inconsistent after 2nd level of nesting.
I perused the code, but am not familiar enough with things to spot anything.
Thanks for the original patch. Made my code less ugly.
Textile Reference I followed said to do pre and code.
Let’s try just code so you can see the html. I also took out lt and gt.
form action="/inventory/requests/350903690" class="edit_request" id="edit_request_350903690" method="post"> input id="request_existing_line_item_attributes_51161156_equipment_type_id" name="request[existing_line_item_attributes][51161156][equipment_type_id]" size="30" type="text" value="691081111" / input id="request_existing_line_item_attributes___existing_equipment_line_item_attributes_860256615_equipment_id" name="request[existing_line_item_attributes[]][existing_equipment_line_item_attributes][860256615][equipment_id]" size="30" type="text" value="1029003634" / /form