ruby-on-railshttp-redirectcancellationbutton-to

How do I create a button that simply redirects without submitting my form?


I’m using Rails 4.2.7. I want to add a “Cancel” button on my form that will simply direct the user to another part of the application. So I implemented

<%= button_to "Cancel", {:controller => :my_objects, :action => 'index' }, :class => 'btn' %>

but this renders as a submit button and the form that this button appears in gets submitted when it is clicked, which is not what I want. How do I change the above so that the button is just rendered as a button that directs the user somewhere else (without form submission)?


Solution

  • You should be using a link_to instead.

    <%= link_to "Cancel", { controller: "my_objects", action: "index", class: "btn" } %>