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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris N. (Guest)
on 2006-04-19 17:29
(Received via mailing list)
Hi,

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
to
do the same with AJAX, it is not working. Whichever button we press, we
get
the value of the first button on the form being passed as a parameter to
the
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.
Is
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
in
the right direction or recommend a good book on AJAX?

Any help would be appreciated. Thanks,

cn
Edward F. (Guest)
on 2006-04-19 18:00
(Received via mailing list)
Chris,

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

Ed
Michael G. (Guest)
on 2006-04-19 18:08
Edward F. 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}'}
end

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.