Multiple submits with remote forms

I’m sure many of you have run across this one. Using ajax forms in rails
with multiple submits always sends the value of the first defined submit
tag rather than the one that is clicked. This is actually against rfc
and I have read the open trac ticket and they are just trying to figure
out the best way to solve it since the problem is actually with the
serialization in prototype. Anyway, I use this little trick to get past
it and it works well so I thought I would share.

Add a hidden field to the form and a simple onclick action to the submit

<% form_remote_tag :url => {:action => ‘dispatch_to_wanted_method’} do
<%= hidden_field_tag :commit_choice %>
… (rest of form)
<%= submit_tag ‘Choice 1’, :onclick =>
“$(‘commit_choice’).value=this.value” %>
<%= submit_tag ‘Choice 2’, :onclick =>
“$(‘commit_choice’).value=this.value” %>
<% end -%>

Then, use an if or case statement in your action to look at the value of
params[:commit_choice] instead of the actual commit buttons.

Hope this helps save someone some time.



William P.