<?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>matthewcarriere.com &#187; ajax</title>
	<atom:link href="http://matthewcarriere.com/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthewcarriere.com</link>
	<description>a blog about startups, tech, and development.</description>
	<lastBuildDate>Sat, 12 May 2012 04:31:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>using text_field_with_auto_complete and form_authenticity_token</title>
		<link>http://matthewcarriere.com/2008/08/13/using-text_field_with_auto_complete-and-form_authenticity_token/</link>
		<comments>http://matthewcarriere.com/2008/08/13/using-text_field_with_auto_complete-and-form_authenticity_token/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 06:14:39 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=31</guid>
		<description><![CDATA[Rails 2.0 introduced protection for Cross-site request forgery (CSRF). This is turned on by default in your Rails applications. However, many developers are turning off this valuable protection rather than making the changes necessary to use it. This seems the same to me as leaving your door unlocked while your friend visits because cutting keys [...]]]></description>
			<content:encoded><![CDATA[<p>Rails 2.0 introduced protection for <a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">Cross-site request forgery (CSRF)</a>. This is turned on by default in your Rails applications. However, many developers are turning off this valuable protection rather than making the changes necessary to use it. This seems the same to me as leaving your door unlocked while your friend visits because cutting keys is hard. Case in point: text_field_with_auto_complete</p>
<p>You know you have run into the new CSRF protection when you see this error in your Rails application:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActionController::InvalidAuthenticityToken</span></pre></div></div>

<p>This is the point where you Google the error and receive one of the following pieces of advice:</p>
<h4>1. Turn off RequestForgeryProtection all together</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># add the following line to your application.rb or one of your</span>
<span style="color:#008000; font-style:italic;"># environment.rb files. why not production? (sarcasm)</span>
config.<span style="color:#9900CC;">action_controller</span>.<span style="color:#9900CC;">allow_forgery_protection</span> = <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

<h4>2. Turn off RequestForgeryProtection for just one controller</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">skip_before_filter <span style="color:#ff3333; font-weight:bold;">:verify_authenticity_token</span></pre></div></div>

<h4>3. Turn off RequestForgeryProtection for just one action.</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">protect_from_forgery <span style="color:#ff3333; font-weight:bold;">:except</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:attack_me</span></pre></div></div>

<p>So let&#8217;s see how easy it is to utilize this new feature!</p>
<p>I was working with the text_field_with_auto_complete in a pretty basic form:</p>
<p>For those of you who aren&#8217;t familiar with this there is a nice post on the <a href="http://wiki.rubyonrails.com/rails/pages/How+to+use+text_field_with_auto_complete">Rails Wiki</a></p>
<p>When I tried to perform my auto complete I received the <em>ActionController::InvalidAuthenticityToken</em> error.</p>
<p>Why? Because basically my Ajax request did not include a security token that RequestForgeryProtection uses to validate my request is coming from the right place. All you need to do is add this token to the parameters sent with your request. Rails makes this easy with the helper: form_authenticity_token. The token name RequestForgeryProtection will be looking for is: authenticity_token.</p>
<p>So let&#8217;s add this to our text_field_with_auto_complete:</p>
<p>Now when I performed my auto complete it worked flawlessly! Now to refactor that string&#8230; any suggestions?</p>
<h4>Update August 25, 2008</h4>
<p>Learned two things working with this&#8230;</p>
<p>1. It&#8217;s automatically doing a POST, so by changing the method to GET I can avoid having to deal with the authenticity_token.</p>
<p>2. And with that out of the way I don&#8217;t need to specify the parameters since by default it will send back the value of the text field.</p>
<p>That&#8217;s a lot cleaner!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/08/13/using-text_field_with_auto_complete-and-form_authenticity_token/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

