Skip to content

Commit 71fb8eb

Browse files
committed
button_to: Support authenticity_token: option
Match support for passing `authenticity_token:` in `form_with` and `form_for` calls. ```ruby button_to "Create", Post.new, authenticity_token: false # => <form class="button_to" method="post" action="/posts"><button type="submit">Create</button></form> button_to "Create", Post.new, authenticity_token: true # => <form class="button_to" method="post" action="/posts"><button type="submit">Create</button><input type="hidden" name="form_token" value="abc123..." autocomplete="off" /></form> button_to "Create", Post.new, authenticity_token: "secret" # => <form class="button_to" method="post" action="/posts"><button type="submit">Create</button><input type="hidden" name="form_token" value="secret" autocomplete="off" /></form> ```
1 parent 9f98066 commit 71fb8eb

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
* Add `:day_format` option to `date_select`
217

318
date_select("article", "written_on", day_format: ->(day) { day.ordinalize })

actionview/lib/action_view/helpers/url_helper.rb

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

335+
authenticity_token = html_options.delete("authenticity_token")
336+
335337
method = html_options.delete("method").to_s
336338
method_tag = BUTTON_TAG_METHOD_VERBS.include?(method) ? method_tag(method) : "".html_safe
337339

@@ -344,7 +346,7 @@ def button_to(name = nil, options = nil, html_options = nil, &block)
344346

345347
request_token_tag = if form_method == "post"
346348
request_method = method.empty? ? "post" : method
347-
token_tag(nil, form_options: { action: url, method: request_method })
349+
token_tag(authenticity_token, form_options: { action: url, method: request_method })
348350
else
349351
""
350352
end
@@ -768,7 +770,12 @@ def method_not_get_method?(method)
768770

769771
def token_tag(token = nil, form_options: {})
770772
if token != false && defined?(protect_against_forgery?) && protect_against_forgery?
771-
token ||= form_authenticity_token(form_options: form_options)
773+
token =
774+
if token == true || token.nil?
775+
form_authenticity_token(form_options: form_options.merge(authenticity_token: token))
776+
else
777+
token
778+
end
772779
tag(:input, type: "hidden", name: request_forgery_protection_token.to_s, value: token, autocomplete: "off")
773780
else
774781
""

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)