Skip to content

Commit 0338221

Browse files
authored
Merge PR rails#43413
2 parents 39a25ba + 7d2be2e commit 0338221

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

actionview/CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
* Infer HTTP verb `[method]` from a model or Array with model as the first
2+
argument to `button_to` when combined with a block:
3+
4+
```ruby
5+
button_to(Workshop.find(1)){ "Update" }
6+
#=> <form method="post" action="/workshops/1" class="button_to">
7+
#=> <input type="hidden" name="_method" value="patch" autocomplete="off" />
8+
#=> <button type="submit">Update</button>
9+
#=> </form>
10+
11+
button_to([ Workshop.find(1), Session.find(1) ]) { "Update" }
12+
#=> <form method="post" action="/workshops/1/sessions/1" class="button_to">
13+
#=> <input type="hidden" name="_method" value="patch" autocomplete="off" />
14+
#=> <button type="submit">Update</button>
15+
#=> </form>
16+
```
17+
18+
*Sean Doyle*
19+
120
* Support passing a Symbol as the first argument to `FormBuilder#button`:
221

322
```ruby

actionview/lib/action_view/helpers/url_helper.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def button_to(name = nil, options = nil, html_options = nil, &block)
346346

347347
authenticity_token = html_options.delete("authenticity_token")
348348

349-
method = html_options.delete("method").to_s
349+
method = (html_options.delete("method").presence || method_for_options(options)).to_s
350350
method_tag = BUTTON_TAG_METHOD_VERBS.include?(method) ? method_tag(method) : "".html_safe
351351

352352
form_method = method == "get" ? "get" : "post"
@@ -767,6 +767,16 @@ def add_method_to_attributes!(html_options, method)
767767
html_options["data-method"] = method
768768
end
769769

770+
def method_for_options(options)
771+
if options.is_a?(Array)
772+
method_for_options(options.last)
773+
elsif options.respond_to?(:persisted?)
774+
options.persisted? ? :patch : :post
775+
elsif options.respond_to?(:to_model)
776+
method_for_options(options.to_model)
777+
end
778+
end
779+
770780
STRINGIFIED_COMMON_METHODS = {
771781
get: "get",
772782
delete: "delete",

actionview/test/template/url_helper_test.rb

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ class UrlHelperTest < ActiveSupport::TestCase
3535
get "/other" => "foo#other"
3636
get "/article/:id" => "foo#article", :as => :article
3737
get "/category/:category" => "foo#category"
38-
resources :workshops
38+
resources :sessions
39+
resources :workshops do
40+
resources :sessions
41+
end
3942

4043
scope :engine do
4144
get "/" => "foo#bar"
@@ -208,6 +211,62 @@ def test_button_to_with_false_url_and_block
208211
)
209212
end
210213

214+
def test_button_to_with_new_record_model
215+
session = Session.new(nil)
216+
217+
assert_dom_equal(
218+
%{<form method="post" action="/sessions" class="button_to"><button type="submit">Create Session</button></form>},
219+
button_to("Create Session", session)
220+
)
221+
end
222+
223+
def test_button_to_with_new_record_model_and_block
224+
workshop = Workshop.new(nil)
225+
226+
assert_dom_equal(
227+
%{<form method="post" action="/workshops" class="button_to"><button type="submit">Create</button></form>},
228+
button_to(workshop) { "Create" }
229+
)
230+
end
231+
232+
def test_button_to_with_nested_new_record_model_and_block
233+
workshop = Workshop.new("1")
234+
session = Session.new(nil)
235+
236+
assert_dom_equal(
237+
%{<form method="post" action="/workshops/1/sessions" class="button_to"><button type="submit">Create</button></form>},
238+
button_to([workshop, session]) { "Create" }
239+
)
240+
end
241+
242+
def test_button_to_with_persisted_model
243+
workshop = Workshop.new("1")
244+
245+
assert_dom_equal(
246+
%{<form method="post" action="/workshops/1" class="button_to"><input type="hidden" name="_method" value="patch" autocomplete="off" /><button type="submit">Update</button></form>},
247+
button_to(workshop) { "Update" }
248+
)
249+
end
250+
251+
def test_button_to_with_persisted_model_and_block
252+
workshop = Workshop.new("1")
253+
254+
assert_dom_equal(
255+
%{<form method="post" action="/workshops/1" class="button_to"><input type="hidden" name="_method" value="patch" autocomplete="off" /><button type="submit">Update</button></form>},
256+
button_to(workshop) { "Update" }
257+
)
258+
end
259+
260+
def test_button_to_with_nested_persisted_model_and_block
261+
workshop = Workshop.new("1")
262+
session = Session.new("1")
263+
264+
assert_dom_equal(
265+
%{<form method="post" action="/workshops/1/sessions/1" class="button_to"><input type="hidden" name="_method" value="patch" autocomplete="off" /><button type="submit">Update</button></form>},
266+
button_to([workshop, session]) { "Update" }
267+
)
268+
end
269+
211270
def test_button_to_with_straight_url_and_request_forgery
212271
self.request_forgery = true
213272

0 commit comments

Comments
 (0)