Skip to content

Commit 15f6113

Browse files
Support name Symbol to FormBuilder#button
Since `<button>` elements translate their `[name]` and `[value]` attributes to the resulting `<form>` element submission, and are encoded into the resulting `URLSearchParams` or `FormData` instance, Action View `FormBuilder` instances should support encoding a method name the same way it does for other fields. For instance, consider this HTML: ```html <button>Publish</button> <button name="post[draft]" value="true">Save as draft</button> ``` Clicking the "Publish" button would submit the form without encoding any additional `[name]` and `[value]` pairs. Clicking the "Save as draft" button would submit the form and encode `post[draft]=true` into the submission. This commit changes the `FormBuilder#button` method to interpret a `Symbol` as the first argument as a method name argument, and encodes its value based on the form's `model:` or `scope:` value: ```erb <%= form.button :draft, value: true do %> Save as draft <% end %> end <%# => <button name="post[draft]" value="true" type="submit"> %> <%# Save as draft %> <%# </button> %> ``` Co-authored-by: Rafael Mendonça França <[email protected]>
1 parent 876d2ff commit 15f6113

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

actionview/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
* Support passing a Symbol as the first argument to `FormBuilder#button`:
2+
3+
```ruby
4+
form.button(:draft, value: true)
5+
# => <button name="post[draft]" value="true" type="submit">Create post</button>
6+
7+
form.button(:draft, value: true) do
8+
content_tag(:strong, "Save as draft")
9+
end
10+
# => <button name="post[draft]" value="true" type="submit">
11+
# <strong>Save as draft</strong>
12+
# </button>
13+
```
14+
15+
*Sean Doyle*
16+
117
* Introduce the `field_name` view helper, along with the
218
`FormBuilder#field_name` counterpart:
319

actionview/lib/action_view/helpers/form_helper.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2597,6 +2597,9 @@ def submit(value = nil, options = {})
25972597
# button("Create post")
25982598
# # => <button name='button' type='submit'>Create post</button>
25992599
#
2600+
# button(:draft, value: true)
2601+
# # => <button name="post[draft]" value="true" type="submit">Create post</button>
2602+
#
26002603
# button do
26012604
# content_tag(:strong, 'Ask me!')
26022605
# end
@@ -2611,8 +2614,20 @@ def submit(value = nil, options = {})
26112614
# # <strong>Create post</strong>
26122615
# # </button>
26132616
#
2617+
# button(:draft, value: true) do
2618+
# content_tag(:strong, "Save as draft")
2619+
# end
2620+
# # => <button name="post[draft]" value="true" type="submit">
2621+
# # <strong>Save as draft</strong>
2622+
# # </button>
2623+
#
26142624
def button(value = nil, options = {}, &block)
2615-
value, options = nil, value if value.is_a?(Hash)
2625+
case value
2626+
when Hash
2627+
value, options = nil, value
2628+
when Symbol
2629+
value, options[:name] = nil, field_name(value)
2630+
end
26162631
value ||= submit_default_value
26172632

26182633
if block_given?

actionview/test/template/form_helper_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,30 @@ def test_submit_with_object_which_is_namespaced
25322532
end
25332533
end
25342534

2535+
def test_button_with_method_name
2536+
form_for(@post) do |f|
2537+
concat f.button(:secret, value: true)
2538+
end
2539+
2540+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", method: "patch") do
2541+
%(<button type="submit" name="post[secret]" value="true">Update Post</button>)
2542+
end
2543+
2544+
assert_dom_equal expected, output_buffer
2545+
end
2546+
2547+
def test_button_with_method_name_and_block
2548+
form_for(@post) do |f|
2549+
concat f.button(:secret, value: true) { "Update secret Post" }
2550+
end
2551+
2552+
expected = whole_form("/posts/123", "edit_post_123", "edit_post", method: "patch") do
2553+
%(<button type="submit" name="post[secret]" value="true">Update secret Post</button>)
2554+
end
2555+
2556+
assert_dom_equal expected, output_buffer
2557+
end
2558+
25352559
def test_button_with_get_formmethod_attribute
25362560
form_for(@post, as: :another_post) do |f|
25372561
concat f.button "GET", formmethod: :get

0 commit comments

Comments
 (0)