Rails: remote_form_for doesn't play 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 options presented would reflect the button the button was clicked on. However, no matter which button I click, the options presented include "commit"=>"Foo" .

What am I doing wrong?

+6
ajax ruby-on-rails
source share
4 answers

form_remote_tag generates some Javascript that uses the Prototype Form.serialize method to send form field values ​​to the server. Form.serialize always uses the first element, which has the name "commit", so it is always "Foo".

As a working moment, you can add a hidden field and specify the tags in your tag and 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();" %> 
+5
source share

Instead of using a single form with two submit tags, you can use either 2x button_to_remote or 2x link_to_remote :

 <%= button_to_remote "Foo", :url => { :action => :wotsit } %> <%= button_to_remote "Bah", :url => { :action => :wotsit } %> 

I think button_to_remote sends "Foo" or "Bah". link_to_remote definitely not . But then you can use the :with parameter to send something as a parameter.

Check the PrototypeHelper documentation for this.

+1
source share

try it

View

 <% form_remote_tag(:url => {:controller => "test", :action => "test_123"},:html => {:id => "form_test"}) do %> <%= hidden_field_tag "submit" %> <%= submit_tag "Restore", :id => 'restore', :value => "", :onclick => "$('download').disabled = true;$('delete').disabled = true;$('submit').value='restore';" %> <%= submit_tag "Download", :id => 'download', :value => "", :onclick => "$('restore').disabled = true;$('delete').disabled = true;$('submit').value='download';" %> <%= submit_tag "Delete", :id => 'delete', :value => "", :onclick => "$('restore').disabled = true;$('download').disabled = true;$('submit').value='delete';" %> <% end %> 

controller

  def test_123 case params[:submit] when "restore" when "download" when "delete" end end 
+1
source share

IMHO this is a cleaning agent ...

 <%= hidden_field_tag "real_commit", "" %> <%= f.submit "Foo", :OnClick => "$('#real_commit')[0].value='Foo'" %> <%= f.submit "Bar", :OnClick => "$('#real_commit')[0].value='Bar'" %> 
0
source share

All Articles