Merge pull request #43886 from seanpdoyle/form-builder-button-method-name-id

Generate `[id]` for `FormBuilder#button` called with method name
This commit is contained in:
Rafael Mendonça França 2021-12-15 18:12:47 -05:00 committed by GitHub
commit 2a55e3432c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View File

@ -2587,7 +2587,7 @@ module ActionView
# # => <button name='button' type='submit'>Create post</button>
#
# button(:draft, value: true)
# # => <button name="post[draft]" value="true" type="submit">Create post</button>
# # => <button id="post_draft" name="post[draft]" value="true" type="submit">Create post</button>
#
# button do
# content_tag(:strong, 'Ask me!')
@ -2606,7 +2606,7 @@ module ActionView
# button(:draft, value: true) do
# content_tag(:strong, "Save as draft")
# end
# # => <button name="post[draft]" value="true" type="submit">
# # => <button id="post_draft" name="post[draft]" value="true" type="submit">
# # <strong>Save as draft</strong>
# # </button>
#
@ -2615,7 +2615,7 @@ module ActionView
when Hash
value, options = nil, value
when Symbol
value, options[:name] = nil, field_name(value)
value, options = nil, { name: field_name(value), id: field_id(value) }.merge!(options.to_h)
end
value ||= submit_default_value

View File

@ -2607,7 +2607,19 @@ class FormHelperTest < ActionView::TestCase
end
expected = whole_form("/posts/123", "edit_post_123", "edit_post", method: "patch") do
%(<button type="submit" name="post[secret]" value="true">Update Post</button>)
%(<button type="submit" id="post_secret" name="post[secret]" value="true">Update Post</button>)
end
assert_dom_equal expected, output_buffer
end
def test_button_with_method_name_and_attributes
form_for(@post) do |f|
concat f.button(:secret, value: true, id: "not_generated", name: "post[not_generated]")
end
expected = whole_form("/posts/123", "edit_post_123", "edit_post", method: "patch") do
%(<button type="submit" id="not_generated" name="post[not_generated]" value="true">Update Post</button>)
end
assert_dom_equal expected, output_buffer
@ -2619,7 +2631,7 @@ class FormHelperTest < ActionView::TestCase
end
expected = whole_form("/posts/123", "edit_post_123", "edit_post", method: "patch") do
%(<button type="submit" name="post[secret]" value="true">Update secret Post</button>)
%(<button type="submit" id="post_secret" name="post[secret]" value="true">Update secret Post</button>)
end
assert_dom_equal expected, output_buffer