Form w/ two submit buttons

I want to have a form with a save and a cancel button. According to several Web site I should be able to put these two tags shown below in my form:

<input type="submit" value="Save" name="savecancel"/> <input type="submit" value="Cancel" name="savecancel"/>

I expect to be able to determine which button is clicked by testing 'params[:savecancel] == 'Cancel'. However, it doesn't work, both values are in the request. I.e., Firebug reports the request as:

_method=put&comment%5Bsummary%5D=Comment&comment%5Bdescription%5D=description4&tag_names=&savecancel=Save&savecancel=Cancel&previous_page=%2Ftasks%2Fsearch%3Fpage%3D2

And Webrick reports:

  Parameters: {"previous_page"=>"/tasks/search?page=2", "_method"=>"put", "action"=>"update", "savecancel"=>"Save", "id"=>"532", "controller"=>"comments", "tag_names"=>"", "taggable_id"=>"531", "comment"=>{"summary"=>"Comment", "description"=>"description4"}}

Regardless of which button is actually clicked. Suggestions? Is Prototype.js messing this up?

TIA,   Jeffrey

Quoting Ryan Bigg (Radar) <radarlistener@gmail.com>:

Is this in an ajax form?

The ajax form will only detect the first submit tag.

Is a remote_form_for an AJAX form? If so, yes this is.

Jeffrey

I found the same problem with image buttons. Prototype does not pass along the .x typically submitted by a normal HTML form.

Suggestions? No one has been able to offer me any concrete ones. It's a severe limitation in prototype (or perhaps XHR in general).

-- gw

It’s something that you’ll have on all ajax frameworks, there are a few possible solutions to the problem:

  1. http://www.rails.cz/articles/2007/07/13/ajax-forms-with-multiple-submit-buttons-bug

  2. A cancel button is not something that should submit the form values in the first place, cancelling is just an action and not an update, so it’s better to just keep it a (either normal or ajax) link, i.e. . You can then style it to appear just like your submit button.

Best regards

Peter De Berdt

Quoting Jeffrey L. Taylor <ror@abluz.dyndns.org>:

Quoting Ryan Bigg (Radar) <radarlistener@gmail.com>: > Is this in an ajax form? > > The ajax form will only detect the first submit tag. >

Is a remote_form_for an AJAX form? If so, yes this is.

I have been looking at del.icio.us which is where I stole the idea from. Using the Firebug extension for Firefox I can see the HTML and GET/POST requests and the responses. It's interesting. The "form" isn't really an HTML form, it's a bunch of input tags. The cancel button does not make a request to the server. And the "form" isn't replacing the content being altered, it's still there but hidden, i.e., 'display: none' style/attribute. Should be easy to delete the "form" and unhide the original content with some JavaScript. I don't understand it all, but I have enough to explore. When I have something working I'll write it up on my blog and post a link here.

Jeffrey