Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: remote_form_for not playing nicely with multiple submit_tags

I have a form with the following:

<% form_remote_tag :url => {:action => :wotsit} do %>
  <%= submit_tag "Foo" %>
  <%= submit_tag "Bah" %>
<% end %>

I would expect that the submitted parameters would reflect which button had been clicked. However, no matter which button I click, the submitted parameters include "commit"=>"Foo".

What am I doing wrong?

like image 210
grifaton Avatar asked Apr 23 '10 21:04

grifaton


1 Answers

form_remote_tag generates some Javascript that uses Prototype's Form.serialize method to send the values of your form fields to the server. Form.serialize is always using the first element that has the name "commit", so it's always "Foo".

As a work-around you could add a hidden field & have your submit tags set that field & submit the form:

<%= hidden_field_tag "real_commit", "" %>
<%= button_to_function "Foo", "$('real_commit').value='Foo';$('myform').submit();" %>
<%= button_to_function "Bah", "$('real_commit').value='Bah';$('myform').submit();" %>
like image 59
jdeseno Avatar answered Oct 05 '22 00:10

jdeseno