Dave Dave - 1 year ago 73
Ruby Question

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)?

Answer Source

You should be using a link_to instead.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download