Skip to content

Commit

Permalink
Merge pull request #47436 from seanpdoyle/action-view-nested-field-na…
Browse files Browse the repository at this point in the history
…me-calls

Don't double-encode nested `field_id` and `field_name` index
  • Loading branch information
amatsuda committed Jul 2, 2023
2 parents f46d345 + bceac79 commit f8c06f9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
7 changes: 7 additions & 0 deletions actionview/CHANGELOG.md
@@ -1,3 +1,10 @@
* Don't double-encode nested `field_id` and `field_name` index values

Pass `index: @options` as a default keyword argument to `field_id` and
`field_name` view helper methods.

*Sean Doyle*

* Allow opting in/out of `Link preload` headers when calling `stylesheet_link_tag` or `javascript_include_tag`

```ruby
Expand Down
4 changes: 2 additions & 2 deletions actionview/lib/action_view/helpers/form_helper.rb
Expand Up @@ -1767,7 +1767,7 @@ def id
# <tt>aria-describedby</tt> attribute referencing the <tt><span></tt>
# element, sharing a common <tt>id</tt> root (<tt>post_title</tt>, in this
# case).
def field_id(method, *suffixes, namespace: @options[:namespace], index: @index)
def field_id(method, *suffixes, namespace: @options[:namespace], index: @options[:index])
@template.field_id(@object_name, method, *suffixes, namespace: namespace, index: index)
end

Expand All @@ -1787,7 +1787,7 @@ def field_id(method, *suffixes, namespace: @options[:namespace], index: @index)
# <%# => <input type="text" name="post[tag][]">
# <% end %>
#
def field_name(method, *methods, multiple: false, index: @index)
def field_name(method, *methods, multiple: false, index: @options[:index])
object_name = @options.fetch(:as) { @object_name }

@template.field_name(object_name, method, *methods, index: index, multiple: multiple)
Expand Down
51 changes: 51 additions & 0 deletions actionview/test/template/form_helper_test.rb
Expand Up @@ -1894,6 +1894,57 @@ def test_form_for_field_id_with_namespace_and_index
assert_dom_equal expected, @rendered
end

def test_form_for_with_nested_attributes_field_id
post, comment, tag = Post.new, Comment.new, Tag.new
comment.relevances = [tag]
post.comments = [comment]
form_for(post) do |form|
form.fields_for(:comments) do |comment_form|
concat comment_form.field_id :relevances_attributes
end
end

expected = whole_form("/posts", "new_post", "new_post") do
"post_comments_attributes_0_relevances_attributes"
end

assert_dom_equal expected, @rendered
end

def test_form_for_with_nested_attributes_field_name
post, comment, tag = Post.new, Comment.new, Tag.new
comment.relevances = [tag]
post.comments = [comment]
form_for(post) do |form|
form.fields_for(:comments) do |comment_form|
concat comment_form.field_name :relevances_attributes
end
end

expected = whole_form("/posts", "new_post", "new_post") do
"post[comments_attributes][0][relevances_attributes]"
end

assert_dom_equal expected, @rendered
end

def test_form_for_with_nested_attributes_field_name_multiple
post, comment, tag = Post.new, Comment.new, Tag.new
comment.relevances = [tag]
post.comments = [comment]
form_for(post) do |form|
form.fields_for(:comments) do |comment_form|
concat comment_form.field_name :relevances_attributes, multiple: true
end
end

expected = whole_form("/posts", "new_post", "new_post") do
"post[comments_attributes][0][relevances_attributes][]"
end

assert_dom_equal expected, @rendered
end

def test_form_for_with_collection_radio_buttons
post = Post.new
def post.active; false; end
Expand Down

0 comments on commit f8c06f9

Please sign in to comment.