Skip to content

Commit 6ddd2cf

Browse files
Merge pull request rails#43417 from seanpdoyle/button-to-authenticity-token
button_to: Support `authenticity_token:` option
2 parents e0076e4 + b667e48 commit 6ddd2cf

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

actionview/CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
* Add support for `button_to ..., authenticity_token: false`
2+
3+
```ruby
4+
button_to "Create", Post.new, authenticity_token: false
5+
# => <form class="button_to" method="post" action="/posts"><button type="submit">Create</button></form>
6+
7+
button_to "Create", Post.new, authenticity_token: true
8+
# => <form class="button_to" method="post" action="/posts"><button type="submit">Create</button><input type="hidden" name="form_token" value="abc123..." autocomplete="off" /></form>
9+
10+
button_to "Create", Post.new, authenticity_token: "secret"
11+
# => <form class="button_to" method="post" action="/posts"><button type="submit">Create</button><input type="hidden" name="form_token" value="secret" autocomplete="off" /></form>
12+
```
13+
14+
*Sean Doyle*
15+
116
* Support rendering `<form>` elements _without_ `[action]` attributes by:
217

318
* `form_with url: false` or `form_with ..., html: { action: false }`

actionview/lib/action_view/helpers/url_helper.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ def button_to(name = nil, options = nil, html_options = nil, &block)
344344
remote = html_options.delete("remote")
345345
params = html_options.delete("params")
346346

347+
authenticity_token = html_options.delete("authenticity_token")
348+
347349
method = html_options.delete("method").to_s
348350
method_tag = BUTTON_TAG_METHOD_VERBS.include?(method) ? method_tag(method) : "".html_safe
349351

@@ -356,7 +358,7 @@ def button_to(name = nil, options = nil, html_options = nil, &block)
356358

357359
request_token_tag = if form_method == "post"
358360
request_method = method.empty? ? "post" : method
359-
token_tag(nil, form_options: { action: url, method: request_method })
361+
token_tag(authenticity_token, form_options: { action: url, method: request_method })
360362
else
361363
""
362364
end
@@ -780,7 +782,12 @@ def method_not_get_method?(method)
780782

781783
def token_tag(token = nil, form_options: {})
782784
if token != false && defined?(protect_against_forgery?) && protect_against_forgery?
783-
token ||= form_authenticity_token(form_options: form_options)
785+
token =
786+
if token == true || token.nil?
787+
form_authenticity_token(form_options: form_options.merge(authenticity_token: token))
788+
else
789+
token
790+
end
784791
tag(:input, type: "hidden", name: request_forgery_protection_token.to_s, value: token, autocomplete: "off")
785792
else
786793
""

actionview/test/template/url_helper_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,39 @@ def test_button_to_without_protect_against_forgery_method
150150
self.class.define_method(:protect_against_forgery?) { request_forgery }
151151
end
152152

153+
def test_button_to_with_authenticity_token
154+
self.request_forgery = true
155+
156+
assert_dom_equal(
157+
%{<form method="post" action="http://www.example.com" class="button_to"><button type="submit">Hello</button><input name="form_token" type="hidden" value="token" autocomplete="off" /></form>},
158+
button_to("Hello", "http://www.example.com", authenticity_token: "token")
159+
)
160+
ensure
161+
self.request_forgery = false
162+
end
163+
164+
def test_button_to_with_authenticity_token_true
165+
self.request_forgery = true
166+
167+
assert_dom_equal(
168+
%{<form method="post" action="http://www.example.com" class="button_to"><button type="submit">Hello</button><input name="form_token" type="hidden" value="secret" autocomplete="off" /></form>},
169+
button_to("Hello", "http://www.example.com", authenticity_token: true)
170+
)
171+
ensure
172+
self.request_forgery = false
173+
end
174+
175+
def test_button_to_with_authenticity_token_false
176+
self.request_forgery = true
177+
178+
assert_dom_equal(
179+
%{<form method="post" action="http://www.example.com" class="button_to"><button type="submit">Hello</button></form>},
180+
button_to("Hello", "http://www.example.com", authenticity_token: false)
181+
)
182+
ensure
183+
self.request_forgery = false
184+
end
185+
153186
def test_button_to_with_straight_url
154187
assert_dom_equal %{<form method="post" action="http://www.example.com" class="button_to"><button type="submit">Hello</button></form>}, button_to("Hello", "http://www.example.com")
155188
end

0 commit comments

Comments
 (0)