Two submit buttons

I am using two submit buttons within my form without any problem, as
specified (http://www.railsdiary.com/diary/two_submit_buttons)

BUT when the user doesn’t hit a button and just hit ENTER, I got the
first button submit in my controller which unfortunatly is the Cancel
button in my view…

Yes, if I change the button position (OK first in html…) I get it… but
I’d like to keep my display as required by the users… Cancel button
must come before OK button…

<%= submit_tag "Cancel", {:name => "submit", :class => "inputSubmit" }%> <%= submit_tag "OK»", {:name => "submit", :class => "inputSubmit"} %>

is ther any way to do that ?

thanks a lot fyl

Could you not use CSS to position the buttons in another order?

For example, one of:

div.submit div {
position: relative;
}

/* you would need to define separate classes for the ok and cancel
buttons */

div.submit div .okButton {
position: absolute;
top: 0px;
left: 0px;
}

div.submit div .cancelButton {
position: absolute;
top: 0px;
left: 100px; /* or however wide your buttons are */
}

Alternatively:

div.submit div {
float: right;
}

I haven’t tested either of these, but you should be able to do
something similar to swap the button order.

Cheers,
Roland

On Jan 8, 2:06 pm, Kad K. [email protected]

Try setting the tab order using tabindex:

<%= image_submit_tag( “buttons/ok.gif”,
:border => “0”,
:title => ‘Submit page’,
:id => ‘ok_button’,
:tabindex => 13) %>

Just make sure that your OK button is earlier in the tabindex order than
the Cancel button.

Here are some references:

http://www.w3.org/TR/html4/interact/forms.html#h-17.11.1
http://www.htmlcodetutorial.com/forms/_INPUT_TABINDEX.html

David

On this two submit button subject, have anyone successfully tried the
“:name” technique in a form_remote_tag case? In the following example,
params[:preview] and params[:ok] are both present, so can’t tell which
button is clicked, any idea how to get around this?

<%= form_remote_tag …>
<image_submit_tag(“ok.gif”, :name=>“ok”)%>
<image_submit_tag(“preview.gif”, :name=>“preview”)%>
<%= end_form_tag %>

thanks

Oliver

if i remember correctly, the way around this is to have a hidden field
in the form and when you click the submit button, you set the value on
the hidden field to indicate what button was clicked. you can then
check this hidden field value on the server.

hmm … not sure why this can work. If I understand it correctly, A
hidden field is part of the submitted form, not associated with the
submit button. <%= hidden_field_tag … %> will be submitted along
with the rest of the form, but still not sufficient to make a
distinction. Maybe I get you wrong, an example will be good.

thanks for the reply.

Oliver

off the top of my head…not tested. not even sure if you need the
submit() call in the onclicks as it may still get submitted because of
the submit buttons. give it a shot.

<%= form_remote_tag …, :html => { :id => ‘my_form’ } %>

<%= submit_tag “Delete”, :onclick => “$(‘form_action’).value =
‘delete’;$(‘my_form’).submit()” %>
<%= submit_tag “Update”, :onclick => “$(‘form_action’).value =
‘update’;$('my_form.submit();” %>
<%= hidden_field_tag “form_action”, “”, :id => “form_action” %>
<%= end_form_tag %<