ActionView::Helpers::FormBuilder
def button(value = nil, options = {}, &block)

Description

Creates a type="submit" <button> whose content is based on the outer form's model.

Arguments

Changing a value updates the ERB and HTML.

= nil
Accepts:
  • A string: used as the content of the <button>.
  • A symbol: adds name and id, and the content stays the default. Unlike other helpers, it does not read the object's value and adds no value attribute.
  • A hash: treated as options (so the second argument, options, can be left out).
  • Omitted: the content is the first of the following that exists:
    1. The helpers.submit.article.create translation (update if the record is persisted; likewise below)
    2. The helpers.submit.create translation
    3. Create followed by the model name (model_name.human), as in Create Article
options= {}
"button"
Sets type to the given value as is. Defaults to type="submit".
"commit"
Defaults to name="button". When the value argument is a symbol, builds the name from it. This option overrides both. nil omits name.
"draft_button"
When the value argument is a symbol, builds the id from it. This option overrides it. nil omits id.
"true"
Sets value to the given value as is. By default, no value is added.
Depending on the value:
  • Anything other than post and get, such as :delete or :patch: sets formmethod to post, adds name="_method", and sets value to the given value. Rails reads _method in the incoming POST and handles the request as a DELETE or PATCH.
  • :get and :post: used as formmethod as is.
When the :name or :value option is given, or the first argument, value, is a symbol, any value is used as formmethod as is, without being rewritten.
Common options

Rails handles these options the same way in every helper. Options the HTML spec does not allow on the <button>, and ones this site considers rarely used, are not listed here.

Shared by all helpers

true adds disabled="disabled"; false omits it. The same goes for every HTML attribute in Rails' list (BOOLEAN_ATTRIBUTES), such as readonly, required and autofocus: any value other than false or nil adds it as attr="attr".
{active: true, hidden: false}
A hash gives the keys whose values are not false or nil, joined with spaces. An array gives its elements joined with spaces, after removing nil, false and empty strings (including whitespace-only strings).
{controller: "tags", tags_suggestions_value: ["ruby", "rails"]}
A hash adds data-* attributes, with _ in the keys converted to -. Array and hash values are converted to JSON strings.
{describedby: ["name_help", "name_error"], invalid: false}
Like :data, adds aria-* attributes. Array values are joined with spaces, leaving out nil and empty strings (nothing is added if none remain). For a hash value, the keys whose values are not false or nil are joined with spaces.
Any other options become HTML attributes of the <button>; those with a nil value are left out.
&block= nil
With a block, uses the block's output as the content of the <button>. The block receives (as text) the value argument if it is a string, and otherwise the default content.
Context
The @article passed to the outer form. When the value argument is omitted, the content is Create Article for a new record and Update Article for a persisted one.

ERB

Changes from the previous output: Added Removed Changed


          

HTML


          

Preview

Interacting with the preview updates the form submission.

Form submission

Form data

namevalue
Request body

Content-Type:


              

params


              
This site hides the CSRF token as (hidden). The real token is a long string that changes on every render.