Skip to content

Commit 2a55e34

Browse files
authored
Merge pull request rails#43886 from seanpdoyle/form-builder-button-method-name-id
Generate `[id]` for `FormBuilder#button` called with method name
2 parents 8434312 + c6c138e commit 2a55e34

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Diff for: actionview/lib/action_view/helpers/form_helper.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -2587,7 +2587,7 @@ def submit(value = nil, options = {})
25872587
# # => <button name='button' type='submit'>Create post</button>
25882588
#
25892589
# button(:draft, value: true)
2590-
# # => <button name="post[draft]" value="true" type="submit">Create post</button>
2590+
# # => <button id="post_draft" name="post[draft]" value="true" type="submit">Create post</button>
25912591
#
25922592
# button do
25932593
# content_tag(:strong, 'Ask me!')
@@ -2606,7 +2606,7 @@ def submit(value = nil, options = {})
26062606
# button(:draft, value: true) do
26072607
# content_tag(:strong, "Save as draft")
26082608
# end
2609-
# # => <button name="post[draft]" value="true" type="submit">
2609+
# # => <button id="post_draft" name="post[draft]" value="true" type="submit">
26102610
# # <strong>Save as draft</strong>
26112611
# # </button>
26122612
#
@@ -2615,7 +2615,7 @@ def button(value = nil, options = {}, &block)
26152615
when Hash
26162616
value, options = nil, value
26172617
when Symbol
2618-
value, options[:name] = nil, field_name(value)
2618+
value, options = nil, { name: field_name(value), id: field_id(value) }.merge!(options.to_h)
26192619
end
26202620
value ||= submit_default_value
26212621

Diff for: actionview/test/template/form_helper_test.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -2607,7 +2607,19 @@ def test_button_with_method_name
26072607
end
26082608

26092609
expected = whole_form("/posts/123", "edit_post_123", "edit_post", method: "patch") do
2610-
%(<button type="submit" name="post[secret]" value="true">Update Post</button>)
2610+
%(<button type="submit" id="post_secret" name="post[secret]" value="true">Update Post</button>)
2611+
end
2612+
2613+
assert_dom_equal expected, output_buffer
2614+
end
2615+
2616+
def test_button_with_method_name_and_attributes
2617+
form_for(@post) do |f|
2618+
concat f.button(:secret, value: true, id: "not_generated", name: "post[not_generated]")
2619+
end
2620+
2621+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", method: "patch") do
2622+
%(<button type="submit" id="not_generated" name="post[not_generated]" value="true">Update Post</button>)
26112623
end
26122624

26132625
assert_dom_equal expected, output_buffer
@@ -2619,7 +2631,7 @@ def test_button_with_method_name_and_block
26192631
end
26202632

26212633
expected = whole_form("/posts/123", "edit_post_123", "edit_post", method: "patch") do
2622-
%(<button type="submit" name="post[secret]" value="true">Update secret Post</button>)
2634+
%(<button type="submit" id="post_secret" name="post[secret]" value="true">Update secret Post</button>)
26232635
end
26242636

26252637
assert_dom_equal expected, output_buffer

0 commit comments

Comments
 (0)