Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

button_to without submit

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' %>
like image 968
jojo Avatar asked Jul 17 '11 22:07

jojo


2 Answers

Try the following:

<%= button_tag "Cancel", :type => 'button', :class => "subBtn", :onclick => "location.href = '#{list_admin_users_admin_index_path()}'" %>
like image 156
Beena Shetty Avatar answered Nov 12 '22 14:11

Beena Shetty


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.

like image 11
mu is too short Avatar answered Nov 12 '22 14:11

mu is too short