<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Blah Blah Woof Woof comments on Multiple AJAX submit buttons in a Rails form</title>
    <link>http://vault.openmonkey.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Blah Blah Woof Woof comments</description>
    <item>
      <title>"Multiple AJAX submit buttons in a Rails form": comment by mary</title>
      <description>Gah! You can get full and unadulterated Tourettes from trying to find work-arounds for IE's little "quirks".</description>
      <pubDate>Tue, 17 Jan 2006 02:52:43 PST</pubDate>
      <guid>http://vault.openmonkey.com/articles/2006/01/16/multiple-ajax-submit-buttons-in-a-rails-form#comment-28</guid>
      <link>http://vault.openmonkey.com/articles/2006/01/16/multiple-ajax-submit-buttons-in-a-rails-form#comment-28</link>
    </item>
    <item>
      <title>"Multiple AJAX submit buttons in a Rails form" by triley</title>
      <description>&lt;p&gt;In a Ruby on Rails app, if you have multiple submit buttons in a web form, the value of the button pressed is not serialised with the rest of the form and so it won&amp;#8217;t be available to the controller in the &lt;code&gt;@params&lt;/code&gt; hash.  This is &lt;a href="http://dev.rubyonrails.org/ticket/3231"&gt;a ticket for this&lt;/a&gt; in the Rails bug tracking system, where a workaround for the problem is documented:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
 &amp;lt;%= hidden_field 'update_button', 'Edit' %&amp;gt;
 &amp;lt;%= submit_tag 'Edit', :name =&amp;gt; '_update_button' %&amp;gt;
 &amp;lt;%= submit_tag 'Cancel', :name =&amp;gt; '_update_button', 
   :onclick =&amp;gt; "Form.getInputs(this.form, null, 'update_button')[0].value = 'Cancel'" %&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;A hidden form element (which actually gets serialised) is used to pass the value of the submit buttons to the controller, and the JavaScript code in the onclick event for the second of the buttons above updates the value of the hidden field depending on which button is pressed.&lt;/p&gt;


	&lt;p&gt;I thought this was a pretty nifty workaround, but in my case, I was submitting a form using &lt;span class="caps"&gt;AJAX&lt;/span&gt;, and my buttons were created using &lt;code&gt;submit_to_remote&lt;/code&gt; instead of &lt;code&gt;submit_tag&lt;/code&gt;.  The problem here was that &lt;code&gt;submit_to_remote&lt;/code&gt; does not let you specify an &lt;code&gt;:onclick&lt;/code&gt; option, since the method uses the onclick event internally in order to submit the form using JavaScript (see the code for the method in &lt;a href="http://api.rubyonrails.com/classes/ActionView/Helpers/JavaScriptHelper.html#M000436"&gt;the documentation&lt;/a&gt;).&lt;/p&gt;


	&lt;p&gt;So, in order to have an &lt;span class="caps"&gt;AJAX&lt;/span&gt;-submitted form with multiple submit buttons that actually passes the value of the button clicked to the controller, it was necessary to quickly re-implement &lt;code&gt;submit_to_remote&lt;/code&gt; in the view:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
 &amp;lt;% hidden_field_tag 'per_page', '20' %&amp;gt;
 &amp;lt;% [ '20', '40', '60' ].each do |num| %&amp;gt;

   &amp;lt;% remote_options = {
     :update =&amp;gt; "dock",
     :loading =&amp;gt; "showLoading();",
     :complete =&amp;gt; "hideLoading();",
     :with =&amp;gt; "Form.serialize(this.form)",
     :url =&amp;gt; { :action =&amp;gt; "query" } } %&amp;gt;
   &amp;lt;%= tag("input", {
     :type =&amp;gt; "button",
     :onclick =&amp;gt; "Form.getInputs(this.form, null, 'per_page')[0].value = '#{num}'; #{remote_function(remote_options)}; return false;",
     :name =&amp;gt; "show_num",
     :value =&amp;gt; "Show #{num}" },
  false) %&amp;gt;

 &amp;lt;% end %&amp;gt;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;This way, you can specify the script to update your hidden variable before including the script that was part of &lt;span&gt;submit_to_remote&lt;/span&gt; that is used to submit the form.&lt;/p&gt;


	&lt;p&gt;Using the example above, you now you can refer to &lt;code&gt;@params[:per_page]&lt;/code&gt; in order to get the value of the button that was clicked to submit the form.&lt;/p&gt;


	&lt;p&gt;The whole reason I had to head down this path was in order to work around a silly rendering bug in Internet Explorer that was causing problems for the &lt;a href="http://www.netfox.com.au/"&gt;NetFox&lt;/a&gt; administration interface.  In the end, we decided that this particular workaround was suboptimal and chose a different solution altogether.  Is it any coincidence that my left eye has started to twitch uncontrollably since I started having to fix bugs caused by IE?&lt;/p&gt;

</description>
      <pubDate>Mon, 16 Jan 2006 02:29:00 PST</pubDate>
      <guid>&lt;a href="/articles/2006/01/16/multiple-ajax-submit-buttons-in-a-rails-form"&gt;Multiple AJAX submit buttons in a Rails form&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2006/01/16/multiple-ajax-submit-buttons-in-a-rails-form"&gt;Multiple AJAX submit buttons in a Rails form&lt;/a&gt;</link>
    </item>
  </channel>
</rss>

