18

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' %>
mu is too short
  • 426,620
  • 70
  • 833
  • 800
jojo
  • 13,583
  • 35
  • 90
  • 123

3 Answers3

30

Try the following:

<%= button_tag "Cancel", :type => 'button', :class => "subBtn", :onclick => "location.href = '#{list_admin_users_admin_index_path()}'" %>
Community
  • 1
  • 1
Beena Shetty
  • 3,676
  • 2
  • 28
  • 31
12

The button_to helper actually generates a whole form:

Generates a form containing a single button that submits to the URL created by the set of options.

So that's definitely not what you want. I think you just want to generate a simple <input type="button"> element in your HTML. However, there is no button_tag in FormTagHelper but there is a submit_tag that you can hand a :type option to and that will:

Any other key creates standard HTML options for the tag.

So try this:

submit_tag 'Pancakes', :type => 'button'

where "Pancakes" would be your real label.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
0

You can use helper similar to f.submit:

<%= f.button 'Save' %>

Documentation

Alex Kojin
  • 5,044
  • 2
  • 29
  • 31