<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SciWerks.com &#187; Ruby on Rails</title>
	<atom:link href="http://www.sciwerks.com/blog/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sciwerks.com/blog</link>
	<description>Software for scientists, by scientists</description>
	<lastBuildDate>Fri, 24 Oct 2008 01:02:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to use an auto_complete to validate passwords</title>
		<link>http://www.sciwerks.com/blog/2007/01/31/how-to-use-an-auto_complete-to-validate-passwords/</link>
		<comments>http://www.sciwerks.com/blog/2007/01/31/how-to-use-an-auto_complete-to-validate-passwords/#comments</comments>
		<pubDate>Thu, 01 Feb 2007 02:04:20 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[auto_complete]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2007/01/31/how-to-use-an-auto_complete-to-validate-passwords/</guid>
		<description><![CDATA[As a follow up to my previous article about using auto_completes to ensure unique filenames, I&#8217;ve put together a bit of code that does essentially the same thing except that it checks a password field to ensure that it meets certain criteria.




# view file


&#60;%= text_field_with_auto_complete :user, :password %&#62;&#60;div id=login_error&#62;&#60;/div&#62;


&#160;


# model file


&#160;


def User &#60; ActiveRecord::Base


&#160; validates_length_of [...]]]></description>
			<content:encoded><![CDATA[<p>As a follow up to my previous article about using auto_completes to ensure unique filenames, I&#8217;ve put together a bit of code that does essentially the same thing except that it checks a password field to ensure that it meets certain criteria.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="co1"># view file</span></div>
</li>
<li class="li1">
<div class="de1">&lt;%= text_field_with_auto_complete :user, :password %&gt;&lt;div id=login_error&gt;&lt;/div&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1"># model file</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> User &lt; ActiveRecord::Base</div>
</li>
<li class="li1">
<div class="de1">&nbsp; validates_length_of :password, :<span class="kw1">in</span>=&gt;<span class="nu0">3</span>..<span class="nu0">20</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1"># controller file</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> auto_complete_for_user_password</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @user = User.<span class="me1">new</span><span class="br0">&#40;</span>params<span class="br0">&#91;</span>:user<span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; @user.<span class="me1">valid</span>?&nbsp; <span class="co1"># ensures the errors array is populated</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; password_errors = @user.<span class="me1">errors</span>.<span class="me1">on</span><span class="br0">&#40;</span>&#8216;password&#8217;<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; render :update <span class="kw1">do</span> |page|</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;page.<span class="me1">replace_html</span> &#8216;login_error&#8217;, <span class="br0">&#40;</span>password_errors.<span class="me1">empty</span>? ? <span class="st0">&quot;&quot;</span> : password_errors<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>This will indicate when a password does not meet the validation criteria defined in the model.  You can add custom validations to check the strength of the password itself.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-1072568092760209";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2007/01/31/how-to-use-an-auto_complete-to-validate-passwords/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A quick tip for searching for rails related information</title>
		<link>http://www.sciwerks.com/blog/2007/01/05/a-quick-tip-for-searching-for-rails-related-information/</link>
		<comments>http://www.sciwerks.com/blog/2007/01/05/a-quick-tip-for-searching-for-rails-related-information/#comments</comments>
		<pubDate>Fri, 05 Jan 2007 23:36:24 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2007/01/05/a-quick-tip-for-searching-for-rails-related-information/</guid>
		<description><![CDATA[If you use Firefox, you can use this little trick.

go to Rails Google co-op
right click on the search box.
select &#8216;Add keyword for this search&#8217;
fill out the resulting form and add a keyword like &#8216;rails&#8217;
move focus to the address bar (CMD-L on a Mac)
Type &#8216;rails sciwerks&#8217; on the address bar

You can also do this with rails [...]]]></description>
			<content:encoded><![CDATA[<p>If you use Firefox, you can use this little trick.</p>
<ol>
<li>go to <a href="http://www.google.com/coop/cse?cx=017837420008955394371%3Aenopjbgisno" target=_blank>Rails Google co-op</a></li>
<li>right click on the search box.</li>
<li>select &#8216;Add keyword for this search&#8217;</li>
<li>fill out the resulting form and add a keyword like &#8216;rails&#8217;</li>
<li>move focus to the address bar (CMD-L on a Mac)</li>
<li>Type &#8216;rails sciwerks&#8217; on the address bar</li>
</ol>
<p>You can also do this with rails or ruby API methods.</p>
<p>You could also set up a quicksilver web search for this too.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-1072568092760209";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2007/01/05/a-quick-tip-for-searching-for-rails-related-information/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Passing extra data with an auto_complete</title>
		<link>http://www.sciwerks.com/blog/2006/11/21/passing-extra-data-with-an-auto_complete/</link>
		<comments>http://www.sciwerks.com/blog/2006/11/21/passing-extra-data-with-an-auto_complete/#comments</comments>
		<pubDate>Tue, 21 Nov 2006 19:23:43 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[auto_complete]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/11/21/passing-extra-data-with-an-auto_complete/</guid>
		<description><![CDATA[A quick and easy method to pass additional data to an auto_complete_for method.]]></description>
			<content:encoded><![CDATA[<p>I recently found myself trying to use an auto_complete field to allow the user to select an object from a list of possible objects.  That&#8217;s all fine and dandy, but I wanted to exclude certain values depending on which object I was editing.  There are at least two ways to do this, the first is to stuff the extra data into the session and then read that value from the auto_complete_for method and act appropriately.</p>
<p>I don&#8217;t really like this approach because I don&#8217;t want to clutter up the session with various form parameters.  Thankfully, there is a simple way to do this by once again abusing the built in text_field_with_auto_complete function&#8230;</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="co1"># View</span></div>
</li>
<li class="li1">
<div class="de1">&lt;%= text_field_with_auto_complete &#8216;new_object&#8217;, &#8216;name&#8217;, <span class="br0">&#123;</span> :tabindex=&gt;<span class="nu0">1</span> <span class="br0">&#125;</span>, <span class="br0">&#123;</span>:url=&gt;<span class="br0">&#123;</span>:action=&gt;&#8217;auto_complete_for_object_name&#8217;, :extra=&gt;@object.<span class="me1">extra</span><span class="br0">&#125;</span> %&gt; </div>
</li>
</ol>
</div>
</div>
<p>[In my case, I used the :extra data to exclude certain objects from the auto_complete_for response]</p>
<p>By passing the :url option, you rewrite the URL that the AJAX actions call, enabling you to add extra parameters that can be acted on by the method.  Also note that you can change the name of the action called (in fact, you need to specify it or it will default to the name of the action for the current view&#8230; probably not what you want).  This is handy because it lets you assign the response to a variable with a different name without writing a separate auto_complete_for handler.  In this case, the submitted form will have a &#8216;new_object[name]&#8216; in the params, even though the action called was &#8216;auto_complete_for_object_name&#8217;.</p>
<p>Needless to say, you need to write a custom &#8216;auto_complete_for&#8217; method to handle the extra data.  Note that you could also use this method to handle multiple auto_complete fields. </p>
<p><!--adsense--></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/11/21/passing-extra-data-with-an-auto_complete/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Live Forms.  Part 1: Putting the errors in the right place</title>
		<link>http://www.sciwerks.com/blog/2006/11/05/live-forms-part-1-putting-the-errors-in-the-right-place/</link>
		<comments>http://www.sciwerks.com/blog/2006/11/05/live-forms-part-1-putting-the-errors-in-the-right-place/#comments</comments>
		<pubDate>Mon, 06 Nov 2006 02:33:59 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Forms]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[User Interface]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/11/05/live-forms-part-1-putting-the-errors-in-the-right-place/</guid>
		<description><![CDATA[This is Part 1 of a series describing how to make better forms for Rails.
The standard rails approach to handling form validation errors is to spit out a long list of all validation errors in a box at the top of the form.  The problem with this approach is that there is a separation [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is Part 1 of a series describing how to make better forms for Rails.</em></p>
<p>The standard rails approach to handling form validation errors is to spit out a long list of all validation errors in a box at the top of the form.  The problem with this approach is that there is a separation between the list of errors and the field that spawned them, like this.  </p>
<p><img id="image30" src="http://www.sciwerks.com/blog/wp-content/uploads/2006/11/figure1.gif" alt="Figure 1 - Default Rails Form Layout" /></p>
<p>This can be a bit annoying, particularly in a long form where the error message and the offending form field may not appear on the screen at the same time.  Notice that you have to first read the error message, and then read the titles of all the fields to find the offending one(s) that need to be corrected.  They aren&#8217;t even in the same order as they were specified in the form, which makes it even more confusing.  This is just extra work for the user, particularly if your form is long or complex.</p>
<p>One way to address this problem is to put the appropriate error message right next to the field with the error.  This way, it&#8217;s pretty obvious where the problems are, and even what they are.  You can also generally avoid using the name of the field in the message.<br />
A simple example of this might look like this&#8230;.</p>
<p><img id="image29" src="http://www.sciwerks.com/blog/wp-content/uploads/2006/11/figure2.gif" alt="Figure 2 - Inline Error Messages" /></p>
<p>This still isn&#8217;t pretty, but at least the user knows exactly which fields need correcting, and a little CSS magic can even place the message to the right of the offending field.</p>
<p>So how do we implement this?</p>
<p>Simple&#8230; first go into your view and delete the
<div class="codesnip-container" >error_messages_for &#8216;object&#8217;</div>
<p> line.</p>
<p>Then stick this in your application helper.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="co1"># application_helper.rb</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> error_for<span class="br0">&#40;</span>object, method = <span class="kw2">nil</span>, options=<span class="br0">&#123;</span><span class="br0">&#125;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> method</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; err = instance_variable_get<span class="br0">&#40;</span><span class="st0">&quot;@#{object}&quot;</span><span class="br0">&#41;</span>.<span class="me1">errors</span>.<span class="me1">on</span><span class="br0">&#40;</span>method<span class="br0">&#41;</span>.<span class="me1">to_sentence</span> <span class="kw1">rescue</span> instance_variable_get<span class="br0">&#40;</span><span class="st0">&quot;@#{object}&quot;</span><span class="br0">&#41;</span>.<span class="me1">errors</span>.<span class="me1">on</span><span class="br0">&#40;</span>method<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; err = @errors<span class="br0">&#91;</span><span class="st0">&quot;#{object}&quot;</span><span class="br0">&#93;</span> <span class="kw1">rescue</span> <span class="kw2">nil</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; options.<span class="me1">merge</span>!<span class="br0">&#40;</span>:<span class="kw1">class</span>=&gt;&#8217;fieldWithErrors&#8217;, :id=&gt;<span class="st0">&quot;#{[object,method].compact.join(&#8217;_')}-error&quot;</span>, :style=&gt;<span class="br0">&#40;</span>err ? <span class="st0">&quot;#{options[:style]}&quot;</span> : <span class="st0">&quot;#{options[:style]};display: none;&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; content_tag<span class="br0">&#40;</span><span class="st0">&quot;p&quot;</span>,err || <span class="st0">&quot;&quot;</span>, options <span class="br0">&#41;</span>&nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>Then in your form view, add an &#8216;error_for&#8217; call wherever you need one&#8230;</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="co1"># _form.rhtml</span></div>
</li>
<li class="li1">
<div class="de1">&lt;p&gt;&lt;label <span class="kw1">for</span>=<span class="st0">&quot;code_project_name&quot;</span>&gt;Name&lt;/label&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;%= text_field &#8216;code_project&#8217;, &#8216;name&#8217;&nbsp; %&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;%= error_for &#8216;code_project&#8217;, &#8216;name&#8217; %&gt;&lt;/p&gt; </div>
</li>
</ol>
</div>
</div>
<p>If the model fails a validation test, then it will show the message right next to the field that caused the validation problem.<br />
Also note that if you define an instance variable called @errors containing a hash of field_names and messages, they will also be used.  This is handy for those form fields that don&#8217;t correspond to a model attribute.</p>
<p><em>Stay tuned for Part 2, where I will describe a how to make these error message dynamic with AJAX.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/11/05/live-forms-part-1-putting-the-errors-in-the-right-place/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Unspace &#8211; Endless Pageless: No More Next Page</title>
		<link>http://www.sciwerks.com/blog/2006/10/31/unspace-endless-pageless-no-more-next-page/</link>
		<comments>http://www.sciwerks.com/blog/2006/10/31/unspace-endless-pageless-no-more-next-page/#comments</comments>
		<pubDate>Wed, 01 Nov 2006 02:32:01 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[User Interface]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/10/31/unspace-endless-pageless-no-more-next-page/</guid>
		<description><![CDATA[Pete Forde at Unspace has written an interesting article describing a method for implementing &#8216;pageless&#8217; data display in Rails.  This approach is very interesting from a user interface standpoint, as it removes unnecessary user interactions (i.e., &#8216;next&#8217; or &#8216;previous&#8217; buttons) with the display page.  It does introduce it&#8217;s own problems as it is [...]]]></description>
			<content:encoded><![CDATA[<p>Pete Forde at Unspace has written an interesting article describing a method for implementing &#8216;pageless&#8217; data display in Rails.  This approach is very interesting from a user interface standpoint, as it removes unnecessary user interactions (i.e., &#8216;next&#8217; or &#8216;previous&#8217; buttons) with the display page.  It does introduce it&#8217;s own problems as it is easy for the user to get &#8216;lost&#8217; in the results as the normal reference points for the start and end of the page are constantly moving targets.</p>
<p>This technique is clearly one to keep an eye on for a good user experience.  It is not yet clear to me when this type of interface can be used for maximal benefit, and when conventional paging should be the preferred method.</p>
<p><a href="http://unspace.ca/discover/pageless/">Unspace &#8211; Endless Pageless: No More Next Page</a></p>
<p><!--adsense--></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/10/31/unspace-endless-pageless-no-more-next-page/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My interview regarding user interface design at the CED Unconference.</title>
		<link>http://www.sciwerks.com/blog/2006/10/29/my-first-youtube-interview-w00t/</link>
		<comments>http://www.sciwerks.com/blog/2006/10/29/my-first-youtube-interview-w00t/#comments</comments>
		<pubDate>Mon, 30 Oct 2006 01:29:35 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[User Interface]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/10/29/my-first-youtube-interview-w00t/</guid>
		<description><![CDATA[Here&#8217;s a brief interview I did about user interface design at a recent conference in North Carolina.

Yes, that is really me.
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a brief interview I did about user interface design at a recent conference in North Carolina.</p>
<p><object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/MZPxWUL3a2s"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/MZPxWUL3a2s" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object></p>
<p>Yes, that is really me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/10/29/my-first-youtube-interview-w00t/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Double Submit Protection</title>
		<link>http://www.sciwerks.com/blog/2006/10/23/double-submit-protection/</link>
		<comments>http://www.sciwerks.com/blog/2006/10/23/double-submit-protection/#comments</comments>
		<pubDate>Mon, 23 Oct 2006 04:31:28 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/10/23/double-submit-protection/</guid>
		<description><![CDATA[The ease with which a user can inadvertently or deliberately submit a web form multiple times is a persistent problem with web applications and web forms.

Typically this problem arises when a user attempts to submit a form and the server does not immediately respond to the request. The delay can be due to network traffic, high load, or a particularly complex action that must be completed. In any event, it does not take long before the user begins to wonder if they actually hit the button and they click it again just to make sure.

If your application is not properly designed, this can lead to multiple identical records, or actions being processed more than once. Sometimes with undesirable side effects (like multiple billing).]]></description>
			<content:encoded><![CDATA[<p>The ease with which a user can inadvertently or deliberately submit a web form multiple times is a persistent problem with web applications and web forms.</p>
<p>Typically this problem arises when a user attempts to submit a form and the server does not immediately respond to the request.  The delay can be due to network traffic, high load, or a particularly complex action that must be completed.  In any event, it does not take long before the user begins to wonder if they actually hit the button and they click it again just to make sure.</p>
<p>If your application is not properly designed, this can lead to multiple identical records, or actions being processed more than once.  Sometimes with undesirable side effects (like multiple billing).</p>
<p>There are a couple of solutions to this problem.</p>
<ol>
<li>Warn the user to avoid clicking the button more than once&#8230;. this is a complete cop out on the part of the programmers and is just inviting trouble.
</li>
<li>Disable the submit button when pressed (using AJAX).  This sort of works, but if the server is slow and non-responsive, this is likely to fail too.  This will also fail if the user has JS turned off, or if they reload or go back to the form and resubmit.</li>
<li>Disable the button via javascript.  This works reasonably well, but if the user uses the browser back button, they can press the submit button again, resulting in a double submit.  Again, this will fail if JS is turned off</li>
</ol>
<p>The reality is that all of these solutions have problems and don&#8217;t really address the underlying issue.  The real problem is not that the user is submitting multiple copies of the same request, the problem is that the server is trying to act on all of them.</p>
<p><!--adsense--></p>
<h2>The double_submit_protection plugin for Rails</h2>
<h3>How it works.</h3>
<ol>
<li> User submits a request</li>
<li>If the request is successfully processed, it records the time and the contents of the params
</li>
<li>if a request with an identical params array is processed within a predefined interval it will reject the request or redirect to another page
</li>
</ol>
<p>The user can then do whatever they want and they won&#8217;t be able to send the identical request again until the time interval expires.</p>
<p>If they change the content of the form and resubmit, that request will work.</p>
<h3>Installation</h3>
<div class="codesnip-container" >script/plugin install svn://sciwerks.com/home/sciwerks/public_svn/double_submit_protection/</div>
<h3>Usage:</h3>
<p>First, restart your server.</p>
<p>To protect your entire application from duplicate post requests (non-ajax) you can put the following code in your application controller.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">## application.rb</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">double_submit_protection&nbsp; :method=&gt;:post,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:xhr =&gt; <span class="kw2">false</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:interval =&gt; <span class="nu0">60</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:flash =&gt; <span class="br0">&#123;</span>:warning =&gt; &#8216;Double Submit Detected&#8217;<span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:redirect_to =&gt; <span class="br0">&#123;</span>:action=&gt;&#8217;index&#8217;<span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
<h3>Disclaimer and Tips</h3>
<p>This is beta stuff, so don&#8217;t be surprised if it breaks things.  In particular, it can block some repeated AJAX calls if you don&#8217;t configure it properly.</p>
<p>Note that since it uses a before filter to do it&#8217;s dirty work, you can limit the actions it applies to by passing an parameters like</p>
<div class="codesnip-container" >:only=>['action'], or<br />
:except=>['action']</div>
<p>You may need to adjust the timeout period to suit your needs.  Longer timeouts will make your session object get pretty big.</p>
<p>If you have a datetime_select in your form, it may circumvent any timeout interval.  If this control defaults to the current time, then the plugin will not be able to tell that it is the same because the &#8216;minutes&#8217; field will be changing.  It should work fine against multiple submit presses, but it will have a harder time with refresh-submit cases.  Future versions may have the option to ignore datetimes in the params.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/10/23/double-submit-protection/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Using an auto_complete to ensure uniqueness</title>
		<link>http://www.sciwerks.com/blog/2006/10/09/using-an-auto_complete-to-ensure-uniqueness/</link>
		<comments>http://www.sciwerks.com/blog/2006/10/09/using-an-auto_complete-to-ensure-uniqueness/#comments</comments>
		<pubDate>Mon, 09 Oct 2006 16:33:52 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[auto_complete]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/10/09/using-an-auto_complete-to-ensure-uniqueness/</guid>
		<description><![CDATA[A quick and easy way to indicate if a text field is unique by subverting auto_complete fields for your own <s>nefarious</s> purposes.]]></description>
			<content:encoded><![CDATA[<h2>The Problem</h2>
<p>Occasionally there is a need to have a user enter a unique string.  For example, if you want to ensure that a new user&#8217;s login id is not used by someone else.  The traditional rails way of doing this is to set up a &#8216;validates_uniqueness_of&#8217; validation on the model and then catch the error when you try to save the record.  </p>
<p>From the user&#8217;s point of view, this can be a horrendously painful experience that can be akin to trying to outsmart the Sphinx.</p>
<div class="codesnip-container" >Form: &#8220;please enter a unique login id&#8217;<br />
User: &#8220;john smith&#8221;<br />
Form: &#8220;Wrong, guess again!&#8221;<br />
User: &#8220;jsmith&#8221;<br />
Form: &#8220;Nope, getting warmer&#8221;<br />
User: &#8220;jsmith1&#8243;<br />
Form: &#8220;You wish! Keep trying!&#8221;<br />
User: &#8220;as;ldfkjasd;flkajsdf;lakjsd&#8221;<br />
Form: &#8220;Ha! your id must be only numbers and letters!&#8221;<br />
User: &#8220;!@!$!@!#!@#!@#!@#%*(#^($!!!!!!!&#8221;<br />
Form: &#8220;Sorry, wrong again!&#8221;<br />
* user navigates to youtube *</div>
<p>The biggest problem here is that there is no feedback to the user if the value they selected is valid until they submit.  Some forms will also force you to retype your new password everytime this fails as well.  Double ouch!</p>
<h2>Solutions</h2>
<ol>
<li>offer suggestions for valid responses when it fails the first time.  Not exactly the best, you might as well autogenerate their id for them and hope they don&#8217;t forget it.</li>
<li>
provide feedback with AJAX to let them know if the value is ok.</li>
</ol>
<p>As you might have guessed from the title of the article, I&#8217;m going to show you how to do this with a standard rails auto_complete text field.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="co1"># view file</span></div>
</li>
<li class="li1">
<div class="de1">&lt;%= text_field_with_auto_complete :user, :login_id %&gt;&lt;div id=login_error&gt;&lt;/div&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1"># controller file</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> auto_complete_for_user_user_name</div>
</li>
<li class="li1">
<div class="de1">&nbsp; @user = User.<span class="me1">find_by_user_name</span><span class="br0">&#40;</span>params<span class="br0">&#91;</span>:user<span class="br0">&#93;</span><span class="br0">&#91;</span>:user_name<span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;render :update <span class="kw1">do</span> |page|</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;page.<span class="me1">replace_html</span> &#8216;login_error&#8217;, <span class="br0">&#40;</span>@user ? <span class="st0">&quot;Login id already in use&quot;</span> : <span class="st0">&quot;&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>That&#8217;s it.  Anytime the auto_complete matches a record, it will change the &#8216;login_error&#8217; div to indicate there&#8217;s a problem.</p>
<p>Net result&#8230;. a much better user interface experience and much happier users.<span id="more-21"></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/10/09/using-an-auto_complete-to-ensure-uniqueness/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Another blindingly obvious way to disable a submit button when pressed</title>
		<link>http://www.sciwerks.com/blog/2006/10/08/another-blindingly-obvious-way-to-disable-a-submit-button-when-pressed/</link>
		<comments>http://www.sciwerks.com/blog/2006/10/08/another-blindingly-obvious-way-to-disable-a-submit-button-when-pressed/#comments</comments>
		<pubDate>Sun, 08 Oct 2006 12:32:11 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[User Interface]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/10/08/another-blindingly-obvious-way-to-disable-a-submit-button-when-pressed/</guid>
		<description><![CDATA[Probably the simplest and least problematic way to disable a submit button when pressed.




# put this in your &#8216;application_helper&#8217; file


&#160;


def commit_tag&#40;title, html_options=&#123;&#125;&#41;


&#160; submit_tag title, html_options.merge&#40;:id=&#62;&#8217;commit&#8217;, :onclick=&#62;&#8217;$&#40;&#34;commit&#34;&#41;.disabled=true;&#8217;&#41;


end 




Then just use &#8216;commit_tag&#8217; instead of &#8217;submit_tag&#8217; in your form.
Strictly speaking, you don&#8217;t really need to give the button an &#8216;id&#8217;, but I do because it has the bonus [...]]]></description>
			<content:encoded><![CDATA[<p>Probably the simplest and least problematic way to disable a submit button when pressed.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="co1"># put this in your &#8216;application_helper&#8217; file</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> commit_tag<span class="br0">&#40;</span>title, html_options=<span class="br0">&#123;</span><span class="br0">&#125;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; submit_tag title, html_options.<span class="me1">merge</span><span class="br0">&#40;</span>:id=&gt;&#8217;commit&#8217;, :onclick=&gt;&#8217;$<span class="br0">&#40;</span><span class="st0">&quot;commit&quot;</span><span class="br0">&#41;</span>.<span class="me1">disabled</span>=<span class="kw2">true</span>;&#8217;<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span> </div>
</li>
</ol>
</div>
</div>
<p>Then just use &#8216;commit_tag&#8217; instead of &#8217;submit_tag&#8217; in your form.<br />
Strictly speaking, you don&#8217;t really need to give the button an &#8216;id&#8217;, but I do because it has the bonus side effect of making it easy programmatically disable the button from RJS templates.</p>
<p>One reason this approach is superior (I think) to an AJAX call is that there is less of a chance that an over-enthusiastic user will be able to click the button again before the AJAX response renders.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/10/08/another-blindingly-obvious-way-to-disable-a-submit-button-when-pressed/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Auto disabling the submit button with KRJS</title>
		<link>http://www.sciwerks.com/blog/2006/10/04/auto-disabling-the-submit-button-with-krjs/</link>
		<comments>http://www.sciwerks.com/blog/2006/10/04/auto-disabling-the-submit-button-with-krjs/#comments</comments>
		<pubDate>Thu, 05 Oct 2006 00:52:49 +0000</pubDate>
		<dc:creator>Kevin Olbrich</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://www.sciwerks.com/blog/2006/10/04/auto-disabling-the-submit-button-with-krjs/</guid>
		<description><![CDATA[Benjamin over at RubyOnRailsBlog   was recently wondering about how to painlessly disable a submit button to prevent the user from hitting it multiple times.
This requires some sort of javascript hackery to achieve, but the easiest way I&#8217;ve found to do this is with my trusty old KRJS pocketknife.




# in application controller


&#160;


def on_commit_click


&#160; render [...]]]></description>
			<content:encoded><![CDATA[<p>Benjamin over at <a href="http://www.rubyonrailsblog.com/articles/2006/10/03/disable-submit-button-after-its-pressed">RubyOnRailsBlog</a>   was recently wondering about how to painlessly disable a submit button to prevent the user from hitting it multiple times.</p>
<p>This requires some sort of javascript hackery to achieve, but the easiest way I&#8217;ve found to do this is with my trusty old KRJS pocketknife.</p>
<div class="codesnip-container" >
<div class="codesnip" style="font-family: monospace;">
<ol>
<li class="li1">
<div class="de1"><span class="co1"># in application controller</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> on_commit_click</div>
</li>
<li class="li1">
<div class="de1">&nbsp; render :update |page| <span class="kw1">do</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;page<span class="br0">&#91;</span>&#8216;commit&#8217;<span class="br0">&#93;</span>.<span class="me1">disabled</span> = <span class="kw2">true</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;page &lt;&lt; <span class="st0">&quot;$(&#8217;commit&#8217;).form.submit();&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1"># in the form</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;%= submit_tag &#8216;Submit&#8217;, :id=&gt;&#8217;commit&#8217; %&gt; </div>
</li>
</ol>
</div>
</div>
<p>This will add this behavior for all submit buttons with the &#8216;commit&#8217; id.  The hardest part here is remembering to add the &#8216;commit&#8217; id.</p>
<p>Requires: <a href="http://www.agilewebdevelopment.com/plugins/krjs">KRJS Plugin </a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sciwerks.com/blog/2006/10/04/auto-disabling-the-submit-button-with-krjs/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
