Skip to content

Fix a button_to documentation example#37190

Merged
gmcgibbon merged 1 commit intorails:masterfrom
glacials:fix_a_button_to_example
Sep 13, 2019
Merged

Fix a button_to documentation example#37190
gmcgibbon merged 1 commit intorails:masterfrom
glacials:fix_a_button_to_example

Conversation

@glacials
Copy link
Copy Markdown
Contributor

Summary

Fixes an example in button_to's documentation; the description states button_to only takes a symbol for the method argument:

:method - Symbol of HTTP verb. Supported verbs are :post, :get, :delete, :patch, and :put. By default it will be :post.

but an example just below uses a string:

button_to('Destroy', 'http://www.example.com',
          method: "delete", remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %>

Passing an all-lowercase string incidentally works, so the example is technically functional despite being undefined behavior. But passing an all-uppercase string fails, and this is a pretty common practice when specifying HTTP methods (e.g. DELETE), so I think it's best this example is put in line with the documentation and converted to use a symbol.

Reproduction steps:

rails new buttonto
cd buttonto
rails c
# Prevent an error from calling button_to in the console
> def protect_against_forgery?
>   false
> end
=> :protect_against_forgery?

# Example verbatim (works)
>  button_to('Destroy', 'http://www.example.com', method: "delete", remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' })
=> "<form class=\"button_to\" method=\"post\" action=\"http://www.example.com\" data-remote=\"true\"><input type=\"hidden\" name=\"_method\" value=\"delete\" /><input data-confirm=\"Are you sure?\" data-disable-with=\"loading...\" type=\"submit\" value=\"Destroy\" /></form>"

# Example with uppercase method (fails; renders a form without _method=delete)
>  button_to('Destroy', 'http://www.example.com', method: "DELETE", remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' })
=> "<form class=\"button_to\" method=\"post\" action=\"http://www.example.com\" data-remote=\"true\"><input data-confirm=\"Are you sure?\" data-disable-with=\"loading...\" type=\"submit\" value=\"Destroy\" /></form>"

Other Information

I fell for this example myself and tried to pass an all-uppercase string after seeing the example without reading the options descriptions.

Fixes an example in `button_to`'s documentation; the description states
`button_to` only takes a symbol for the `method` argument, but an
example just below uses a string.

Passing an all-lowercase string incidentally works, so the example is
technically functional despite being undefined behavior. But passing an
all-uppercase string fails, and this is a pretty common practice when
specifying HTTP methods (e.g. `DELETE`), so I think it's best this
example is put in line with the documentation and converted to use
symbols.

I fell for it myself and tried to pass an all-uppercase string after
seeing the example without reading the options descriptions.

Reproduction steps:

```sh
rails new buttonto
cd buttonto
rails c
> def protect_against_forgery?
>   false
> end
=> :protect_against_forgery?
>  button_to('Destroy', 'http://www.example.com', method: "delete", remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' })
=> "<form class=\"button_to\" method=\"post\" action=\"http://www.example.com\" data-remote=\"true\"><input type=\"hidden\" name=\"_method\" value=\"delete\" /><input data-confirm=\"Are you sure?\" data-disable-with=\"loading...\" type=\"submit\" value=\"Destroy\" /></form>"
>  button_to('Destroy', 'http://www.example.com', method: "DELETE", remote: true, data: { confirm: 'Are you sure?', disable_with: 'loading...' })
=> "<form class=\"button_to\" method=\"post\" action=\"http://www.example.com\" data-remote=\"true\"><input data-confirm=\"Are you sure?\" data-disable-with=\"loading...\" type=\"submit\" value=\"Destroy\" /></form>"
```
@rails-bot rails-bot bot added the actionview label Sep 13, 2019
Copy link
Copy Markdown
Member

@gmcgibbon gmcgibbon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! For next time, please use [ci skip] for doc only changes.

@gmcgibbon gmcgibbon merged commit ab38bd0 into rails:master Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants