Forum: Ruby on Rails How to use multiple buttons on the same form with AJAX?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
8465c52f55212f5482b2a5d75e2df794?d=identicon&s=25 Chris Newman (Guest)
on 2006-04-19 15:29
(Received via mailing list)

We are trying to use multiple submit buttons in the same form to tie to
different actions on the server side.

For example:

<input name="commit" type="submit" value="edit">
<input name="commit" type="submit" value="cancel">
<input name="commit" type="submit" value="test configuration">

This works with non-AJAX type requests. However, now that we are trying
do the same with AJAX, it is not working. Whichever button we press, we
the value of the first button on the form being passed as a parameter to
server. It looks like to me the problem might be related to how Rails
generates its buttons with the same name (commit) but different values.
this legal in AJAX? Does AJAX use DOM? Is there a work-around for this?

The parameters hash looks like...

"commit" => "edit"

even if we press the "cancel" or "test configuration" buttons.

I have Googled about but haven't found any answers. Can anyone point me
the right direction or recommend a good book on AJAX?

Any help would be appreciated. Thanks,

41e1579600683eed6c00af9a425268e6?d=identicon&s=25 Edward Frederick (Guest)
on 2006-04-19 16:00
(Received via mailing list)

If I recall, this is a known issue with XHR and you just can't.

A0ed1bbfe42f4f87e6db0a16706246e2?d=identicon&s=25 Michael Greenly (mgreenly)
on 2006-04-19 16:08
Edward Frederick wrote:
> Chris,
> If I recall, this is a known issue with XHR and you just can't.
> Ed

def onclick_button_to( label, *options)
 button_to_function label, %Q{location.href='#{url_for *options}'}

This is in my helper and what I use for my cancel buttons in my
multi-button forms.  You pass the same parameters as you would for
link_to.  In my view it looks like...

<%= form_tag :action => "save" %>
<%= onclick_button_to "cancel", :action => "index" %>
<%= submit_tag 'save' %>
<%= end_form_tag %>

This is non-ajax usage but you would just need to modify the javascript
in the onclick_button_to method to get what you wanted...
This topic is locked and can not be replied to.