button_to
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (-1)
- 2.0.3 (14)
- 2.1.0 (0)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (20)
- 3.0.9 (0)
- 3.1.0 (13)
- 3.2.1 (10)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (33)
- 4.1.8 (8)
- 4.2.1 (-5)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (38)
- 7.1.3.2 (-8)
- 7.1.3.4 (0)
- What's this?
button_to(name = nil, options = nil, html_options = nil, &block)
public
Generates a form containing a single button that submits to the URL created by the set of options. This is the safest method to ensure links that cause changes to your data are not triggered by search bots or accelerators. If the HTML button does not work with your layout, you can also consider using the link_to method with the :method modifier as described in the link_to documentation.
You can control the form and button behavior with html_options. Most values in html_options are passed through to the button element. For example, passing a :class option within html_options will set the class attribute of the button element.
The class attribute of the form element can be set by passing a :form_class option within html_options. It defaults to "button_to" to allow styling of the form and its children.
The form submits a POST request by default. You can specify a different HTTP verb via the :method option within html_options.
Options
The options hash accepts the same options as url_for. To generate a <form> element without an [action] attribute, pass false:
<%= button_to "New", false %> # => "<form method="post" class="button_to"> # <button type="submit">New</button> # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/> # </form>"
Most values in html_options are passed through to the button element, but there are a few special options:
-
:method - Symbol of HTTP verb. Supported verbs are :post, :get, :delete, :patch, and :put. By default it will be :post.
-
:disabled - If set to true, it will generate a disabled button.
-
:data - This option can be used to add custom data attributes.
-
:remote - If set to true, will allow the Unobtrusive JavaScript drivers to control the submit behavior. By default this behavior is an ajax submit.
-
:form - This hash will be form attributes
-
:form_class - This controls the class of the form within which the submit button will be placed
-
:params - Hash of parameters to be rendered as hidden fields within the form.
Data attributes
-
:confirm - This will use the unobtrusive JavaScript driver to prompt with the question specified. If the user accepts, the link is processed normally, otherwise no action is taken.
-
:disable_with - Value of this parameter will be used as the value for a disabled version of the submit button when the form is submitted. This feature is provided by the unobtrusive JavaScript driver.
Examples
<%= button_to "New", action: "new" %> # => "<form method="post" action="/controller/new" class="button_to"> # <button type="submit">New</button> # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/> # </form>" <%= button_to "New", new_article_path %> # => "<form method="post" action="/articles/new" class="button_to"> # <button type="submit">New</button> # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/> # </form>" <%= button_to "New", new_article_path, params: { time: Time.now } %> # => "<form method="post" action="/articles/new" class="button_to"> # <button type="submit">New</button> # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/> # <input type="hidden" name="time" value="2021-04-08 14:06:09 -0500"> # </form>" <%= button_to [:make_happy, @user] do %> Make happy <strong><%= @user.name %></strong> <% end %> # => "<form method="post" action="/users/1/make_happy" class="button_to"> # <button type="submit"> # Make happy <strong><%= @user.name %></strong> # </button> # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/> # </form>" <%= button_to "New", { action: "new" }, form_class: "new-thing" %> # => "<form method="post" action="/controller/new" class="new-thing"> # <button type="submit">New</button> # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/> # </form>" <%= button_to "Create", { action: "create" }, remote: true, form: { "data-type" => "json" } %> # => "<form method="post" action="/images/create" class="button_to" data-remote="true" data-type="json"> # <button type="submit">Create</button> # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/> # </form>" <%= button_to "Delete Image", { action: "delete", id: @image.id }, method: :delete, data: { confirm: "Are you sure?" } %> # => "<form method="post" action="/images/delete/1" class="button_to"> # <input type="hidden" name="_method" value="delete" /> # <button data-confirm='Are you sure?' type="submit">Delete Image</button> # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/> # </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 name='_method' value='delete' type='hidden' /> # <button type='submit' data-disable-with='loading...' data-confirm='Are you sure?'>Destroy</button> # <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/> # </form>" #