button_to without submit

jojo picture jojo · Jul 18, 2011 · Viewed 22k times · Source

Can I have a button in side the form, but do not have the type=submit?

I am doing something special. I have a form,

<%= form_for(@user) do |f| %>

and this form will be rendered based on a value in a session.

In side the form, I have f.submit already and I want to have another button, cancel button, to invoke a method from controller to change the session value if user want to cancel the input.

But if I use button_to, the type of the button will be "submit", when I click on the button, it will submit the whole form. If some of the value are not valid, it will complain. It does not work as the "cancel" button that I expected.

So can I have a button that does not submit the form?

I tried to use submit_tag, but the buttom dosn`t work for me...

<%= submit_tag 'Cancel Edit', :type => 'button', :controller => 'my_account', :action=>'cancel_edit' %>

Answer

Beena Shetty picture Beena Shetty · Nov 20, 2012

Try the following:

<%= button_tag "Cancel", :type => 'button', :class => "subBtn", :onclick => "location.href = '#{list_admin_users_admin_index_path()}'" %>