button_to(name, options = {}, html_options = {}) 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.

The generated FORM element has a class name of button-to to allow styling of the form itself and its children. You can control the form submission and input element behavior using html_options. This method accepts the :method and :confirm modifiers described in the link_to documentation. If no :method modifier is given, it will default to performing a POST operation. You can also disable the button by passing :disabled => true in html_options.

  button_to "New", :action => "new"

Generates the following HTML:

  <form method="post" action="/controller/new" class="button-to">
    <div><input value="New" type="submit" /></div>

If you are using RESTful routes, you can pass the :method to change the HTTP verb used to submit the form.

  button_to "Delete Image", { :action => "delete", :id => @image.id },
            :confirm => "Are you sure?", :method => :delete

Which generates the following HTML:

  <form method="post" action="/images/delete/1" class="button-to">
      <input type="hidden" name="_method" value="delete" />
      <input onclick="return confirm('Are you sure?');"
                value="Delete" type="submit" />
Show source
Register or log in to add new notes.