<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>code tunes</title>
	
	<link>http://codetunes.com</link>
	<description>Web applications, software engineering, Ruby on Rails, Cake PHP, JavaScript, etc.</description>
	<pubDate>Mon, 03 May 2010 11:23:59 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/CodeTunes" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="codetunes" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>CakePHP moved to Github!</title>
		<link>http://codetunes.com/2009/12/11/cakephp-moved-to-github/</link>
		<comments>http://codetunes.com/2009/12/11/cakephp-moved-to-github/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 17:56:00 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[CakePHP]]></category>

		<category><![CDATA[github]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=136</guid>
		<description><![CDATA[After two of the core developers, Nate Abele and Gwoo, left the project lately, many feared the worst. Now it&#8217;s a different story! The CakePHP core team decided to move the project to Github.
I&#8217;d say: finally!
Now it&#8217;s truly open to the community. That&#8217;s because Github makes it so easy to collaborate on open source projects. [...]]]></description>
			<content:encoded><![CDATA[<p>After two of the core developers, Nate Abele and Gwoo, left the project lately, <a title="The end of CakePHP?" href="http://cakebaker.42dh.com/2009/10/23/the-end-of-cakephp/">many</a> feared the worst. Now it&#8217;s a different story! The CakePHP core team decided to move the project to <a title="CakePHP on Github" href="http://github.com/cakephp">Github</a>.</p>
<p>I&#8217;d say: finally!</p>
<p>Now it&#8217;s truly open to the community. That&#8217;s because Github makes it so easy to collaborate on open source projects. All you need to do is to fork a project, merge your changes, and send a pull request to project&#8217;s maintainer. Can&#8217;t imagine more painless process.</p>
<p>What I expect now is a real boost in CakePHP development, with many great features delivered by the community. Just check how other projects benefited from the move, for example <a title="Ruby on Rails" href="http://github.com/rails/rails">Ruby on Rails</a> that has more than 700 forks now. So&#8230;</p>
<p>Let&#8217;s fork it! ;)<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/11/04/uploadpack-easy-and-flexible-way-to-upload-files-with-cakephp/" title="UploadPack - easy and flexible way to upload files with CakePHP">UploadPack - easy and flexible way to upload files with CakePHP</a></li>
<li><a href="http://codetunes.com/2008/09/15/namedscopebehavior-upgrade/" title="NamedScopeBehavior upgrade">NamedScopeBehavior upgrade</a></li>
<li><a href="http://codetunes.com/2008/09/05/named-scope-in-cakephp/" title="Named scope in CakePHP">Named scope in CakePHP</a></li>
<li><a href="http://codetunes.com/2008/08/21/convenient-controllers-callback-methods-with-cakephp/" title="Convenient controller&#8217;s callback methods with CakePHP">Convenient controller&#8217;s callback methods with CakePHP</a></li>
<li><a href="http://codetunes.com/2008/07/11/migration-to-cakephp-rc2/" title="Migration to CakePHP RC2">Migration to CakePHP RC2</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2009%2F12%2F11%2Fcakephp-moved-to-github%2F';
  addthis_title  = 'CakePHP+moved+to+Github%21';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feedads.g.doubleclick.net/~a/nnrUGQb76BnujZ-KuN8O9WrN6cc/0/da"><img src="http://feedads.g.doubleclick.net/~a/nnrUGQb76BnujZ-KuN8O9WrN6cc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/nnrUGQb76BnujZ-KuN8O9WrN6cc/1/da"><img src="http://feedads.g.doubleclick.net/~a/nnrUGQb76BnujZ-KuN8O9WrN6cc/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2009/12/11/cakephp-moved-to-github/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Fixtures without validation with Factory Girl</title>
		<link>http://codetunes.com/2009/11/05/fixtures-without-validation-with-factory-girl/</link>
		<comments>http://codetunes.com/2009/11/05/fixtures-without-validation-with-factory-girl/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 12:29:34 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Plugins]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[Factory Girl]]></category>

		<category><![CDATA[fixtures]]></category>

		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=132</guid>
		<description><![CDATA[Factory Girl is my fixture replacement library of choice. It improves tests readability and maintainability. It&#8217;s also customizable.
There are sometimes situations when you want to create test scenarios that checks how your app is handling invalid data (not user input, but invalid records that already sit in your database). To do this you first need [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/thoughtbot/factory_girl">Factory Girl</a> is my fixture replacement library of choice. It improves tests readability and maintainability. It&#8217;s also customizable.</p>
<p>There are sometimes situations when you want to create test scenarios that checks how your app is handling invalid data (not user input, but invalid records that already sit in your database). To do this you first need to put this invalid data to your db.</p>
<p>You could accomplish this with such line:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@user</span> = Factory<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:user</span>, <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;not a correct email address&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>However, factory girl would raise an exception here, because that&#8217;s the default strategy of creating new fixtures - raise exception if save fails (because of validation errors for example).</p>
<p>Thankfully we can use our own strategy of creating new fixtures, such that does save records without validation.</p>
<p>First let&#8217;s define our new strategy:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Factory::Proxy::CreateWithoutValidation</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Factory::Proxy::Build</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> result
    <span style="color:#0066ff; font-weight:bold;">@instance</span>.<span style="color:#9900CC;">save</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@instance</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Factory
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">create_without_validation</span> <span style="color:#006600; font-weight:bold;">&#40;</span>name, overrides = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    factory_by_name<span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">run</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Proxy::CreateWithoutValidation</span>, overrides<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now we can use it while defining new factory:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">Factory.<span style="color:#9900CC;">define</span> <span style="color:#ff3333; font-weight:bold;">:invalid_user</span>, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> User, <span style="color:#ff3333; font-weight:bold;">:default_strategy</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:create_without_validation</span> <span style="color:#9966CC; font-weight:bold;">do</span> |f|
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And then we can happily create invalid fixtures without any exceptions raised.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2009/05/09/turning-off-auto-timestamping-for-testing-in-rails/" title="Turning off auto timestamping for testing in Rails">Turning off auto timestamping for testing in Rails</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2009%2F11%2F05%2Ffixtures-without-validation-with-factory-girl%2F';
  addthis_title  = 'Fixtures+without+validation+with+Factory+Girl';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feedads.g.doubleclick.net/~a/o5zpHniFKNGiEL_A21s8DpL0GDs/0/da"><img src="http://feedads.g.doubleclick.net/~a/o5zpHniFKNGiEL_A21s8DpL0GDs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/o5zpHniFKNGiEL_A21s8DpL0GDs/1/da"><img src="http://feedads.g.doubleclick.net/~a/o5zpHniFKNGiEL_A21s8DpL0GDs/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2009/11/05/fixtures-without-validation-with-factory-girl/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Turning off auto timestamping for testing in Rails</title>
		<link>http://codetunes.com/2009/05/09/turning-off-auto-timestamping-for-testing-in-rails/</link>
		<comments>http://codetunes.com/2009/05/09/turning-off-auto-timestamping-for-testing-in-rails/#comments</comments>
		<pubDate>Fri, 08 May 2009 22:35:54 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[ActiveRecord]]></category>

		<category><![CDATA[Factory Girl]]></category>

		<category><![CDATA[fixtures]]></category>

		<category><![CDATA[Model]]></category>

		<category><![CDATA[testing]]></category>

		<category><![CDATA[timestamping]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=121</guid>
		<description><![CDATA[Suppose that you implemented a functionality that depends on values of created_at or updated_at fields of your models. How do you test it?
If you use fixtures that reside in test/fixture/*.yml files then there is no problem, because the values you set there for created_at and updated_at fields are saved to the database &#8216;as is&#8217;. So [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose that you implemented a functionality that depends on values of created_at or updated_at fields of your models. How do you test it?</p>
<p>If you use fixtures that reside in test/fixture/*.yml files then there is no problem, because the values you set there for created_at and updated_at fields are saved to the database &#8216;as is&#8217;. So you can easily have an article created one week ago:</p>

<div class="wp_syntax"><div class="code"><pre class="yml" style="font-family:monospace;">article:
  title: What a great day
  created_at: &lt;%= 1.week.ago.to_s(:db) %&gt;
  updated_at: &lt;%= 1.week.ago.to_s(:db) %&gt;</pre></div></div>

<p>However, I don&#8217;t use fixtures files myself. I feel a bit dirty using them ;) I find fixture replacement tools far more maintainable. Namely, I love <a href="http://github.com/thoughtbot/factory_girl" title="Factory Girl">thoughtbot&#8217;s Factory Girl</a>. But here comes the problem. This won&#8217;t work as expected with Factory Girl:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">Factory<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:article</span>, <span style="color:#ff3333; font-weight:bold;">:created_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 1.<span style="color:#9900CC;">week</span>.<span style="color:#9900CC;">ago</span>, <span style="color:#ff3333; font-weight:bold;">:updated_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 1.<span style="color:#9900CC;">week</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>That&#8217;s because ActiveRecord&#8217;s automatic timestamping feature sets Time.now for created_at and updated_at fields overriding our values. At least that&#8217;s ActiveRecord&#8217;s default behavior. Fortunately it can be disabled with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">Article.<span style="color:#9900CC;">record_timestamps</span> = <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

<p>Chances are that after creating a model with a custom timestamp we&#8217;ll want to turn automatic timestamping back on. But turning it off and on in many places in your unit tests would be pretty cumbersome. Wouldn&#8217;t it be cool if you could achieve all of this with a snippet below?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">without_timestamping_of Article <span style="color:#9966CC; font-weight:bold;">do</span>
  Factory<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:article</span>, <span style="color:#ff3333; font-weight:bold;">:created_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 1.<span style="color:#9900CC;">week</span>.<span style="color:#9900CC;">ago</span>, <span style="color:#ff3333; font-weight:bold;">:updated_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 1.<span style="color:#9900CC;">week</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>It turns timestamping off, executes the block and turns timestamping back on. I find it clean and dry. Here&#8217;s the code to place in your test_helper.rb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># test_helper.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span> <span style="color:#008000; font-style:italic;"># or class ActiveSupport::TestCase in Rails 2.3.x</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> without_timestamping_of<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>klasses<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> block_given?
      klasses.<span style="color:#9900CC;">delete_if</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |klass| !klass.<span style="color:#9900CC;">record_timestamps</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      klasses.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |klass| klass.<span style="color:#9900CC;">record_timestamps</span> = <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">begin</span>
        <span style="color:#9966CC; font-weight:bold;">yield</span>
      <span style="color:#9966CC; font-weight:bold;">ensure</span>
        klasses.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |klass| klass.<span style="color:#9900CC;">record_timestamps</span> = <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Of course you can turn off timestamping for many models at once:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">without_timestamping_of Article, Comment, User <span style="color:#9966CC; font-weight:bold;">do</span>
  Factory<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:article</span>, <span style="color:#ff3333; font-weight:bold;">:created_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 1.<span style="color:#9900CC;">week</span>.<span style="color:#9900CC;">ago</span>, <span style="color:#ff3333; font-weight:bold;">:updated_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 1.<span style="color:#9900CC;">week</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  Factory<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:comment</span>, <span style="color:#ff3333; font-weight:bold;">:created_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 1.<span style="color:#9900CC;">day</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  Factory<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:user</span>, <span style="color:#ff3333; font-weight:bold;">:updated_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 5.<span style="color:#9900CC;">hours</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Hope you like it. If so, share :)<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2009/11/05/fixtures-without-validation-with-factory-girl/" title="Fixtures without validation with Factory Girl">Fixtures without validation with Factory Girl</a></li>
<li><a href="http://codetunes.com/2008/07/20/tableless-models-in-rails/" title="Tableless models in Rails">Tableless models in Rails</a></li>
<li><a href="http://codetunes.com/2009/03/27/websecurity-through-conventions-and-best-practices/" title="Websecurity through conventions and best practices">Websecurity through conventions and best practices</a></li>
<li><a href="http://codetunes.com/2009/03/23/speaking-at-it-underground-security-conference-and-workshop-in-prague/" title="Speaking at IT Underground Security Conference and Workshop in Prague">Speaking at IT Underground Security Conference and Workshop in Prague</a></li>
<li><a href="http://codetunes.com/2009/01/31/rails-222-ajax-and-respond_to/" title="Rails 2.2.2, Ajax and respond_to">Rails 2.2.2, Ajax and respond_to</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2009%2F05%2F09%2Fturning-off-auto-timestamping-for-testing-in-rails%2F';
  addthis_title  = 'Turning+off+auto+timestamping+for+testing+in+Rails';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feedads.g.doubleclick.net/~a/P4_qZU2aah5z95sHhzzCcw6LQWU/0/da"><img src="http://feedads.g.doubleclick.net/~a/P4_qZU2aah5z95sHhzzCcw6LQWU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/P4_qZU2aah5z95sHhzzCcw6LQWU/1/da"><img src="http://feedads.g.doubleclick.net/~a/P4_qZU2aah5z95sHhzzCcw6LQWU/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2009/05/09/turning-off-auto-timestamping-for-testing-in-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Dynamic cookie domains with Rack’s middleware</title>
		<link>http://codetunes.com/2009/04/17/dynamic-cookie-domains-with-racks-middleware/</link>
		<comments>http://codetunes.com/2009/04/17/dynamic-cookie-domains-with-racks-middleware/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 13:26:52 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Rack]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[cookies]]></category>

		<category><![CDATA[middleware]]></category>

		<category><![CDATA[multi-domain]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=117</guid>
		<description><![CDATA[Handling sessions in multi-domain environment is not the simplest things to do, because of the fact that cookies are scoped to a domain they were set by.
Recently we were developing an application with such an idea in mind:

Application will work as a base for other mini-applications (which we call sites)
Each site can be accessed via [...]]]></description>
			<content:encoded><![CDATA[<p>Handling sessions in multi-domain environment is not the simplest things to do, because of the fact that cookies are scoped to a domain they were set by.</p>
<p>Recently we were developing an application with such an idea in mind:</p>
<ul>
<li>Application will work as a base for other mini-applications (which we call sites)</li>
<li>Each site can be accessed via different url types: site.example.org and example.org/site</li>
<li>We want the users to remain logged in when switching from one url type to another</li>
</ul>
<p>I won&#8217;t be covering application structure, routing, etc. here, I will only write about maintaing the sessions is such an environment.</p>
<p>So this is pretty simple here - all that we needed to do was to set cookie domain to <strong>.example.org</strong> (note the &#8220;dot&#8221; at the beginning). This could be done via:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>.<span style="color:#9900CC;">session</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:domain</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;.example.org&quot;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>However there was an additional requirement that we need to deal with:</p>
<ul>
<li>Each site can be accessed via custom domain - site.com</li>
<li>Of course there&#8217;s no way here to keep the user logged in when he&#8217;s switching from site.com to example.org/site or site.example.org, at least it cannot be done with setting cookie domain to whatever value</li>
</ul>
<p>Technically, to access the site via site.com, that domain must point to our IP address. Then we need to detect that the site is being accessed via custom domain and set cookie domain respectively.</p>
<p>This could be done via some funky before_filters in an Application Controller, however we found much better and cleaner way.</p>
<p><strong>Rack&#8217;s middleware to the rescue</strong></p>
<p>Rack itself is a minimal interface between web server and your ruby framework. It&#8217;s used by Ruby on Rails (since 2.3) and Merb. The request comes from web server, goes through middleware layers and enters the application.</p>
<p>So we wrote a middleware layer that detects the host with which our application is accessed and sets cookie domain for the request. Here it is:</p>
<p><strong>app/middlewares/set_cookie_domain.rb</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SetCookieDomain
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>app, default_domain<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@app</span> = app
    <span style="color:#0066ff; font-weight:bold;">@default_domain</span> = default_domain
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> call<span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
    host = env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;HTTP_HOST&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">':'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">first</span>
    env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;rack.session.options&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:domain</span><span style="color:#006600; font-weight:bold;">&#93;</span> = custom_domain?<span style="color:#006600; font-weight:bold;">&#40;</span>host<span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#996600;">&quot;.#{host}&quot;</span> : <span style="color:#996600;">&quot;#{@default_domain}&quot;</span>
    <span style="color:#0066ff; font-weight:bold;">@app</span>.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> custom_domain?<span style="color:#006600; font-weight:bold;">&#40;</span>host<span style="color:#006600; font-weight:bold;">&#41;</span>
    domain = <span style="color:#0066ff; font-weight:bold;">@default_domain</span>.<span style="color:#CC0066; font-weight:bold;">sub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^\.<span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    host !~ <span style="color:#CC00FF; font-weight:bold;">Regexp</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{domain}$&quot;</span>, <span style="color:#6666ff; font-weight:bold;">Regexp::IGNORECASE</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now we need to turn it on:</p>
<p> <strong>environment.rb</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">load_paths</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006600; font-weight:bold;">%</span>W<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#008000; font-style:italic;">#{RAILS_ROOT}/app/middlewares )</span></pre></div></div>

<p><strong>production.rb</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">use</span> <span style="color:#996600;">&quot;SetCookieDomain&quot;</span>, <span style="color:#996600;">&quot;.example.org&quot;</span></pre></div></div>

<p>.example.org is the default domain that will be used unless the application is accessed via custom domain (like site.com), we give it different values depending on environment (production/staging/development etc).</p>
<p>And since we&#8217;re fans of test driven development, here&#8217;s the test that ensures us that everything works as expected:</p>
<p><strong>tests/integration/set_cookie_domain_test.rb</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'test_helper'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> SetCookieDomainTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::IntegrationTest</span>
&nbsp;
  context <span style="color:#996600;">&quot;when accessing site at example.org&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    setup <span style="color:#9966CC; font-weight:bold;">do</span>
      host! <span style="color:#996600;">'example.org'</span>
      visit <span style="color:#996600;">'/'</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    should <span style="color:#996600;">&quot;set cookie_domain to .example.org&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      assert_equal <span style="color:#996600;">'.example.org'</span>, <span style="color:#0066ff; font-weight:bold;">@integration_session</span>.<span style="color:#9900CC;">controller</span>.<span style="color:#9900CC;">request</span>.<span style="color:#9900CC;">session_options</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:domain</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  context <span style="color:#996600;">&quot;when accessing site at site.com&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    setup <span style="color:#9966CC; font-weight:bold;">do</span>
      host! <span style="color:#996600;">'site.com'</span>
      visit <span style="color:#996600;">'/'</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    should <span style="color:#996600;">&quot;set cookie_domain to .site.com&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      assert_equal <span style="color:#996600;">'.site.com'</span>, <span style="color:#0066ff; font-weight:bold;">@integration_session</span>.<span style="color:#9900CC;">controller</span>.<span style="color:#9900CC;">request</span>.<span style="color:#9900CC;">session_options</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:domain</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  context <span style="color:#996600;">&quot;when accessing site at site.example.org&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    setup <span style="color:#9966CC; font-weight:bold;">do</span>
      host! <span style="color:#996600;">'site.example.org'</span>
      visit <span style="color:#996600;">'/'</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    should <span style="color:#996600;">&quot;set cookie_domain to .example.org&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      assert_equal <span style="color:#996600;">'.example.org'</span>, <span style="color:#0066ff; font-weight:bold;">@integration_session</span>.<span style="color:#9900CC;">controller</span>.<span style="color:#9900CC;">request</span>.<span style="color:#9900CC;">session_options</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:domain</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Test is sponsored by great <a title="Shoulda" href="http://thoughtbot.com/projects/shoulda/">Shoulda</a> and <a title="Webrat" href="http://github.com/brynary/webrat/tree/master">Webrat</a> gems.</p>
<p>Feel free to comment and share.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li>No Related Post</li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2009%2F04%2F17%2Fdynamic-cookie-domains-with-racks-middleware%2F';
  addthis_title  = 'Dynamic+cookie+domains+with+Rack%26%238217%3Bs+middleware';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feedads.g.doubleclick.net/~a/iG7Hc-op3u1bfvD-rheMBZmjcS0/0/da"><img src="http://feedads.g.doubleclick.net/~a/iG7Hc-op3u1bfvD-rheMBZmjcS0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/iG7Hc-op3u1bfvD-rheMBZmjcS0/1/da"><img src="http://feedads.g.doubleclick.net/~a/iG7Hc-op3u1bfvD-rheMBZmjcS0/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2009/04/17/dynamic-cookie-domains-with-racks-middleware/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Websecurity through conventions and best practices</title>
		<link>http://codetunes.com/2009/03/27/websecurity-through-conventions-and-best-practices/</link>
		<comments>http://codetunes.com/2009/03/27/websecurity-through-conventions-and-best-practices/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 15:02:15 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Conferences]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[ITUnderground]]></category>

		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=114</guid>
		<description><![CDATA[As promised few days ago, I publish materials from a presentation I gave at IT Underground Conference in Prague, Czech Republic.
Here&#8217;s a slideshow with full speaker&#8217;s notes. Spread the word ;)
Websecurity through conventions and best practices
View more documents from Michał Szajbe.

Related posts

Speaking at IT Underground Security Conference and Workshop in Prague
Turning off auto timestamping for [...]]]></description>
			<content:encoded><![CDATA[<p>As promised few days ago, I publish materials from a presentation I gave at IT Underground Conference in Prague, Czech Republic.</p>
<p>Here&#8217;s a slideshow with full speaker&#8217;s notes. Spread the word ;)</p>
<div style="width:477px;text-align:left" id="__ss_1205949"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/szajbus/websecurity-through-conventions-and-best-practices?type=document" title="Websecurity through conventions and best practices">Websecurity through conventions and best practices</a><object style="margin:0px" width="477" height="510"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayerd.swf?doc=websec2-090326184603-phpapp01&#038;stripped_title=websecurity-through-conventions-and-best-practices" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayerd.swf?doc=websec2-090326184603-phpapp01&#038;stripped_title=websecurity-through-conventions-and-best-practices" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="477" height="510"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">documents</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/szajbus">Michał Szajbe</a>.</div>
</div>
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2009/03/23/speaking-at-it-underground-security-conference-and-workshop-in-prague/" title="Speaking at IT Underground Security Conference and Workshop in Prague">Speaking at IT Underground Security Conference and Workshop in Prague</a></li>
<li><a href="http://codetunes.com/2009/05/09/turning-off-auto-timestamping-for-testing-in-rails/" title="Turning off auto timestamping for testing in Rails">Turning off auto timestamping for testing in Rails</a></li>
<li><a href="http://codetunes.com/2009/01/31/rails-222-ajax-and-respond_to/" title="Rails 2.2.2, Ajax and respond_to">Rails 2.2.2, Ajax and respond_to</a></li>
<li><a href="http://codetunes.com/2009/01/10/materials-from-aiesec-conference-in-czestochowa-poland/" title="Materials from AIESEC Conference in Częstochowa, Poland">Materials from AIESEC Conference in Częstochowa, Poland</a></li>
<li><a href="http://codetunes.com/2008/12/08/rails-ajax-and-jquery/" title="Rails, Ajax and jQuery">Rails, Ajax and jQuery</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2009%2F03%2F27%2Fwebsecurity-through-conventions-and-best-practices%2F';
  addthis_title  = 'Websecurity+through+conventions+and+best+practices';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feedads.g.doubleclick.net/~a/Xghl1S-PSVOcof7Wi0MuBZHyd-0/0/da"><img src="http://feedads.g.doubleclick.net/~a/Xghl1S-PSVOcof7Wi0MuBZHyd-0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Xghl1S-PSVOcof7Wi0MuBZHyd-0/1/da"><img src="http://feedads.g.doubleclick.net/~a/Xghl1S-PSVOcof7Wi0MuBZHyd-0/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2009/03/27/websecurity-through-conventions-and-best-practices/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Speaking at IT Underground Security Conference and Workshop in Prague</title>
		<link>http://codetunes.com/2009/03/23/speaking-at-it-underground-security-conference-and-workshop-in-prague/</link>
		<comments>http://codetunes.com/2009/03/23/speaking-at-it-underground-security-conference-and-workshop-in-prague/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 11:00:36 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Conferences]]></category>

		<category><![CDATA[ITUnderground]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=111</guid>
		<description><![CDATA[Tomorrow I will be speaking at IT Underground Security Conference and Workshop in Prague, Czech Republic. That is three day long event that starts today.
I will give a talk on &#8220;Websecurity through conventions and best practices&#8221; which will be focused on securing web applications from the most common attacks with examples from Ruby on Rails [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.itunderground.org/prt/view/prague2009.html"><img class="alignright size-full wp-image-112" title="it_underground_prague_125x125_en" src="http://codetunes.com/wp-content/uploads/2009/03/it_underground_prague_125x125_en.gif" alt="it_underground_prague_125x125_en" width="125" height="125" /></a>Tomorrow I will be speaking at IT Underground Security Conference and Workshop in Prague, Czech Republic. That is three day long event that starts today.</p>
<p>I will give a talk on &#8220;Websecurity through conventions and best practices&#8221; which will be focused on securing web applications from the most common attacks with examples from Ruby on Rails web development framework.</p>
<p>Expect to see materials from the conf here in few days.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2009/03/27/websecurity-through-conventions-and-best-practices/" title="Websecurity through conventions and best practices">Websecurity through conventions and best practices</a></li>
<li><a href="http://codetunes.com/2009/05/09/turning-off-auto-timestamping-for-testing-in-rails/" title="Turning off auto timestamping for testing in Rails">Turning off auto timestamping for testing in Rails</a></li>
<li><a href="http://codetunes.com/2009/01/31/rails-222-ajax-and-respond_to/" title="Rails 2.2.2, Ajax and respond_to">Rails 2.2.2, Ajax and respond_to</a></li>
<li><a href="http://codetunes.com/2009/01/10/materials-from-aiesec-conference-in-czestochowa-poland/" title="Materials from AIESEC Conference in Częstochowa, Poland">Materials from AIESEC Conference in Częstochowa, Poland</a></li>
<li><a href="http://codetunes.com/2008/12/08/rails-ajax-and-jquery/" title="Rails, Ajax and jQuery">Rails, Ajax and jQuery</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2009%2F03%2F23%2Fspeaking-at-it-underground-security-conference-and-workshop-in-prague%2F';
  addthis_title  = 'Speaking+at+IT+Underground+Security+Conference+and+Workshop+in+Prague';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feedads.g.doubleclick.net/~a/Wu_ZO5G6Xe2umf6om_y_JSP4DLs/0/da"><img src="http://feedads.g.doubleclick.net/~a/Wu_ZO5G6Xe2umf6om_y_JSP4DLs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Wu_ZO5G6Xe2umf6om_y_JSP4DLs/1/da"><img src="http://feedads.g.doubleclick.net/~a/Wu_ZO5G6Xe2umf6om_y_JSP4DLs/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2009/03/23/speaking-at-it-underground-security-conference-and-workshop-in-prague/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Rails 2.2.2, Ajax and respond_to</title>
		<link>http://codetunes.com/2009/01/31/rails-222-ajax-and-respond_to/</link>
		<comments>http://codetunes.com/2009/01/31/rails-222-ajax-and-respond_to/#comments</comments>
		<pubDate>Fri, 30 Jan 2009 22:07:15 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Ajax]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=106</guid>
		<description><![CDATA[As I wrote some time ago in the article about Rails, Ajax and jQuery, sometimes there are problems with Rails not interpreting correctly content type headers of ajax requests. It&#8217;s because not all web browsers send that header in the same way. 
What I proposed was to sort the request.accepts array (array containing content type headers [...]]]></description>
			<content:encoded><![CDATA[<p>As I wrote some time ago in the article about <a title="Rails, Ajax and jQuery" href="http://codetunes.com/2008/12/08/rails-ajax-and-jquery/">Rails, Ajax and jQuery</a>, sometimes there are problems with Rails not interpreting correctly content type headers of ajax requests. It&#8217;s because not all web browsers send that header in the same way. </p>
<p>What I proposed was to sort the request.accepts array (array containing content type headers sent by browser) so that xml content type would be the first element. That would then trigger format.xml in our respond_to block.</p>
<p>However that approach does not work in Rails 2.2.2, because now the request.accepts array is frozen and it cannot be modified. I spent some time googling for the solution, but with no effect. So I dived into the API and Rails&#8217; source code and came up with pretty nice and simple solution to the problem.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ApplicationController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
  before_filter <span style="color:#ff3333; font-weight:bold;">:xhr_to_xml</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> xhr_to_xml
    request.<span style="color:#CC0066; font-weight:bold;">format</span> = <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#9966CC; font-weight:bold;">if</span> request.<span style="color:#9900CC;">xhr</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This piece of code is an equivalent of the snippet I proposed in the article I referred to at the beginning. Now all ajax request will trigger format.xml in respond_to blocks.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/12/08/rails-ajax-and-jquery/" title="Rails, Ajax and jQuery">Rails, Ajax and jQuery</a></li>
<li><a href="http://codetunes.com/2009/05/09/turning-off-auto-timestamping-for-testing-in-rails/" title="Turning off auto timestamping for testing in Rails">Turning off auto timestamping for testing in Rails</a></li>
<li><a href="http://codetunes.com/2009/03/27/websecurity-through-conventions-and-best-practices/" title="Websecurity through conventions and best practices">Websecurity through conventions and best practices</a></li>
<li><a href="http://codetunes.com/2009/03/23/speaking-at-it-underground-security-conference-and-workshop-in-prague/" title="Speaking at IT Underground Security Conference and Workshop in Prague">Speaking at IT Underground Security Conference and Workshop in Prague</a></li>
<li><a href="http://codetunes.com/2008/07/30/custom-thumbnail-generation-with-paperclip/" title="Custom thumbnail generation with Paperclip">Custom thumbnail generation with Paperclip</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2009%2F01%2F31%2Frails-222-ajax-and-respond_to%2F';
  addthis_title  = 'Rails+2.2.2%2C+Ajax+and+respond_to';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feedads.g.doubleclick.net/~a/w0npvXRsQOs1eqr2yJ3TlPrYbKY/0/da"><img src="http://feedads.g.doubleclick.net/~a/w0npvXRsQOs1eqr2yJ3TlPrYbKY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/w0npvXRsQOs1eqr2yJ3TlPrYbKY/1/da"><img src="http://feedads.g.doubleclick.net/~a/w0npvXRsQOs1eqr2yJ3TlPrYbKY/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2009/01/31/rails-222-ajax-and-respond_to/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Materials from AIESEC Conference in Częstochowa, Poland</title>
		<link>http://codetunes.com/2009/01/10/materials-from-aiesec-conference-in-czestochowa-poland/</link>
		<comments>http://codetunes.com/2009/01/10/materials-from-aiesec-conference-in-czestochowa-poland/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 19:05:42 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Conferences]]></category>

		<category><![CDATA[aiesec]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=103</guid>
		<description><![CDATA[Last month the netguru team has been invited to Corporate Responsibility and Information Technology Conference organized by AIESEC (international student organization) which took place in Częstochowa, Poland. We went there and gave some talks on various topics and I think some of you may find materials from this conference quite interesting.
First, we led an unofficial [...]]]></description>
			<content:encoded><![CDATA[<p>Last month the netguru team has been invited to Corporate Responsibility and Information Technology Conference organized by AIESEC (international student organization) which took place in Częstochowa, Poland. We went there and gave some talks on various topics and I think some of you may find materials from this conference quite interesting.</p>
<p>First, we led an unofficial discussion on &#8220;The (fading) differences between desktop and web applications&#8221;. We introduced the topic to our hearers and then let them express themselves, it was quite successful.</p>
<p>On Day 2, we organized Ruby on Rails workshops which consisted of <a title="Introduction to Ruby on Rails" href="http://www.slideshare.net/bartosz/ruby-on-rails-presentation-846628/">&#8220;Introduction to RoR&#8221; talk</a> and one-hour live-coding session during which a simple twitter like application has been developed. You can find the results on <a title="Mini-twitter one-hour app" href="http://github.com/netguru/aiesec-mini-twitter/tree/master">our Github account</a>.</p>
<p>We finished Day 2 with a talk on Enterprise 2.0 and on Day 3 we talked about &#8220;Developing a web-development company (startup)&#8221;, which was quite different from what people heard on earlier talks gave by the representatives of big corporations. I have no materials to share on this, though. Sorry.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2009/03/27/websecurity-through-conventions-and-best-practices/" title="Websecurity through conventions and best practices">Websecurity through conventions and best practices</a></li>
<li><a href="http://codetunes.com/2009/03/23/speaking-at-it-underground-security-conference-and-workshop-in-prague/" title="Speaking at IT Underground Security Conference and Workshop in Prague">Speaking at IT Underground Security Conference and Workshop in Prague</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2009%2F01%2F10%2Fmaterials-from-aiesec-conference-in-czestochowa-poland%2F';
  addthis_title  = 'Materials+from+AIESEC+Conference+in+Cz%C4%99stochowa%2C+Poland';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feedads.g.doubleclick.net/~a/cb5nmU95LQga_-2xPye2azPmdvo/0/da"><img src="http://feedads.g.doubleclick.net/~a/cb5nmU95LQga_-2xPye2azPmdvo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/cb5nmU95LQga_-2xPye2azPmdvo/1/da"><img src="http://feedads.g.doubleclick.net/~a/cb5nmU95LQga_-2xPye2azPmdvo/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2009/01/10/materials-from-aiesec-conference-in-czestochowa-poland/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Devguru blog is now in english</title>
		<link>http://codetunes.com/2009/01/10/devguru-blog-is-now-in-english/</link>
		<comments>http://codetunes.com/2009/01/10/devguru-blog-is-now-in-english/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 15:53:36 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=98</guid>
		<description><![CDATA[Devguru is a blog managed by netguru, a web development &#38; consulting company I work for based in Poznań, Poland. For more that a year all posts on that blog were written in polish, but the good news is that at the beginning of 2009 we decided to switch to english language.
I recommend that you [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://netguru.pl/en"><img class="alignright size-full wp-image-99" style="border: 0px initial initial;" title="netgurupl" src="http://codetunes.com/wp-content/uploads/2009/01/netgurupl.jpg" alt="netgurupl" width="187" height="57" /></a><a title="Devguru" href="http://devguru.pl">Devguru</a> is a blog managed by <a title="netguru" href="http://netguru.pl/en">netguru</a>, a web development &amp; consulting company I work for based in Poznań, Poland. For more that a year all posts on that blog were written in polish, but the good news is that at the beginning of 2009 we decided to <strong>switch to english</strong> language.</p>
<p>I recommend that you take a look on it. The topics covered there are related to web development and programming so it&#8217;s worth giving it a shot. 10 authors - ruby and php developers, html/css coders, designers and so on. </p>
<p>I also write for devguru so most post that you&#8217;ll find here  on codetunes will be also published there and vice versa. Happy reading.<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2008/07/23/welcome-to-code-tunes/" title="Welcome to Code Tunes">Welcome to Code Tunes</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2009%2F01%2F10%2Fdevguru-blog-is-now-in-english%2F';
  addthis_title  = 'Devguru+blog+is+now+in+english';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feedads.g.doubleclick.net/~a/jy0ivg_VdID-mki1n7Q1xeDOHpg/0/da"><img src="http://feedads.g.doubleclick.net/~a/jy0ivg_VdID-mki1n7Q1xeDOHpg/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/jy0ivg_VdID-mki1n7Q1xeDOHpg/1/da"><img src="http://feedads.g.doubleclick.net/~a/jy0ivg_VdID-mki1n7Q1xeDOHpg/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2009/01/10/devguru-blog-is-now-in-english/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Rails, Ajax and jQuery</title>
		<link>http://codetunes.com/2008/12/08/rails-ajax-and-jquery/</link>
		<comments>http://codetunes.com/2008/12/08/rails-ajax-and-jquery/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 15:41:48 +0000</pubDate>
		<dc:creator>Michał Szajbe</dc:creator>
		
		<category><![CDATA[Ajax]]></category>

		<category><![CDATA[Ruby on Rails]]></category>

		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://codetunes.com/?p=89</guid>
		<description><![CDATA[The more ajaxified application, the more fun it is to use. But it is also more painful do develop. What is written below is my approach to pairing Rails and Ajax. It&#8217;s a mix of tips I found over the net on blogs and forums. I use jQuery for JavaScript, but I don&#8217;t use jRails [...]]]></description>
			<content:encoded><![CDATA[<p>The more ajaxified application, the more fun it is to use. But it is also more painful do develop. What is written below is my approach to pairing Rails and Ajax. It&#8217;s a mix of tips I found over the net on blogs and forums. I use jQuery for JavaScript, but I don&#8217;t use jRails or any JS/Ajax helper methods provided by Rails. Note that all Javascript/HTML code presented here can be used even if you dont use Rails or Ruby as your web development platform. Let&#8217;s begin.</p>
<h3>Rails is RESTful</h3>
<p>Thanks to Rails&#8217; RESTfulness the only thing to take care of server side is setting proper response in controllers&#8217; actons.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> PostsController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> index
    <span style="color:#0066ff; font-weight:bold;">@posts</span> = Post.<span style="color:#9900CC;">find</span> <span style="color:#ff3333; font-weight:bold;">:all</span>
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> |format|
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@posts</span>.<span style="color:#9900CC;">to_xml</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Rails decides which format block to call basing on routes defined in routes.rb file (map.connect &#8216;:controller/:action/:id.:format&#8217;) and accept headers sent with request by the client.</p>
<p>In most cases we want Ajax requests to trigger format.xml blocks in our controllers&#8217; actions, so we need to set proper accept headers. Let&#8217;s do it just once with application-wide setting.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// All ajax requests will trigger the format.xml block</span>
<span style="color: #006600; font-style: italic;">// of +respond_to do |format|+ declarations</span>
$.<span style="color: #660066;">ajaxSetup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">'beforeSend'</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xhr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>xhr.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Accept&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;text/xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h3>Browsers&#8217; quirks</h3>
<p>There is something worth noting here, a problem I had once with IE and Safari. The code above may work differently in various browsers. Browser set text/html accept header by default. Here IE and Safari will append text/xml to it so you&#8217;ll get something like &#8216;text/html; text/xml&#8217;, while Firefox will replace text/html with text/xml and you&#8217;ll get &#8216;text/xml&#8217; only. This is very important because Rails will take the first format it detects in accept header and trigger respective block in controller&#8217;s action, which will be html for IE and Safari. Here&#8217;s a fix for this that shifts application/xml (if it is present) to the beggining of accept headers array.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ApplicationController <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
  before_filter <span style="color:#ff3333; font-weight:bold;">:correct_safari_and_ie_accept_headers</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> correct_safari_and_ie_accept_headers
    request.<span style="color:#9900CC;">accepts</span>.<span style="color:#9900CC;">sort</span>!<span style="color:#006600; font-weight:bold;">&#123;</span> |x, y| y.<span style="color:#9900CC;">to_s</span> == <span style="color:#996600;">'application/xml'</span> ? <span style="color:#006666;">1</span> : <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">if</span> request.<span style="color:#9900CC;">xhr</span>?
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>Ajaxify your links</h3>
<p>Here&#8217;s a quick way to ajaxify your existing links. Add this JavaScript to your application.js file.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// All A tags with class 'get', 'post', 'put' or 'delete' will perform an ajax call</span>
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.get'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">livequery</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;
    $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>link.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>link.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ajaxtarget'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        jQuery<span style="color: #009900;">&#40;</span>link.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ajaxtarget'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rel&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;nofollow&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.post'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">livequery</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;
    $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_method=post&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>link.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ajaxtarget'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        jQuery<span style="color: #009900;">&#40;</span>link.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ajaxtarget'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rel&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;nofollow&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.put'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">livequery</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;
    $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_method=put&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>link.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ajaxtarget'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        jQuery<span style="color: #009900;">&#40;</span>link.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ajaxtarget'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rel&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;nofollow&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.delete'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">livequery</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>;
    $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;_method=delete&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>link.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ajaxtarget'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        jQuery<span style="color: #009900;">&#40;</span>link.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ajaxtarget'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rel&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;nofollow&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.get, a.post, a.put, a.delete'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeAttr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'onclick'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Just add a CSS class .get, .post, .delete, or .put to a link to make turn it into an ajax-link. I recommend you use <a title="Livequery" href="http://docs.jquery.com/Plugins/livequery">LiveQuery plugin</a> which will automatically bind click events to new links that appear on the page (loaded with Ajax call for-example). You can optionally set ajaxtarget attibute of the link. It expects a selector of a container in which you want to place the response.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">link_to <span style="color:#996600;">'my cool article'</span>, article_path<span style="color:#006600; font-weight:bold;">&#40;</span>@article<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'get'</span>, <span style="color:#ff3333; font-weight:bold;">:ajaxtarget</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'#article_container'</span></pre></div></div>

<h3>Ajaxify your forms</h3>
<p>For this you&#8217;d need <a title="jQuery Form Plugin" href="http://malsup.com/jquery/form">jQuery Form Plugin</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form.ajax'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">livequery</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'submit'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ajaxSubmit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Now all your forms that have &#8220;ajax&#8221; class will be submitted via Ajax.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form class=&quot;ajax&quot;&gt;
  ...
&lt;/form&gt;</pre></div></div>

<h3>CSRF and authenticity token</h3>
<p>Rails has built-in protection from cross-site request forgery attacks. It relies on an authenticity token which Rails look for when dealing with POST, PUT or DELETE requests, so this token needs to be sent by the browser together with the request. The token is automatically added as a hidden field to any form you create with form_for method, it is also attached to links that have :method param set to :post, :put or :delete. In fact the token is added dynamically by Javascript code placed in link&#8217;s onclick attribute. However in one of code snippets above we stripped that onclick attribute from links to prevent the page reload after we click the link. Now we need to attack that token ourselves. First we will alter our application layout:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;head&gt;
  &lt;% if protect_against_forgery? %&gt;
    &lt;script type='text/javascript'&gt;
    //&lt;![CDATA[
      window._auth_token_name = &quot;#{request_forgery_protection_token}&quot;;
      window._auth_token = &quot;#{form_authenticity_token}&quot;;
    //]]&gt;
    &lt;/script&gt;
  &lt;% end %&gt;
&lt;/head&gt;</pre></div></div>

<p>Now we need to ensure that the token is sent together with ajax requests.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// All non-GET requests will add the authenticity token</span>
  <span style="color: #006600; font-style: italic;">// if not already present in the data packet</span>
  jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;body&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ajaxSend&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elm<span style="color: #339933;">,</span> xhr<span style="color: #339933;">,</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s.<span style="color: #660066;">type</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s.<span style="color: #660066;">data</span> <span style="color: #339933;">&amp;&amp;</span> s.<span style="color: #660066;">data</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>b&quot;</span> <span style="color: #339933;">+</span> window._auth_token_name <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>s.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      s.<span style="color: #660066;">data</span> <span style="color: #339933;">=</span> s.<span style="color: #660066;">data</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&amp;&quot;</span>;
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      s.<span style="color: #660066;">data</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
      <span style="color: #006600; font-style: italic;">// if there was no data, $ didn't set the content-type</span>
      xhr.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-Type&quot;</span><span style="color: #339933;">,</span> s.<span style="color: #660066;">contentType</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
    s.<span style="color: #660066;">data</span> <span style="color: #339933;">=</span> s.<span style="color: #660066;">data</span> <span style="color: #339933;">+</span> encodeURIComponent<span style="color: #009900;">&#40;</span>window._auth_token_name<span style="color: #009900;">&#41;</span>
                    <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> encodeURIComponent<span style="color: #009900;">&#40;</span>window._auth_token<span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>We&#8217;re done, we have our ajax requests protected from CSRF attacks.</p>
<h3>Modifing page after Ajax calls</h3>
<p>Standard way to do page modification after Ajax call is to use Javascript code that inserts content returned by the call somewhere on the page. The other method is to put the modifying code in views that are returned by the server and just execute it in the browser. For this I&#8217;d recommend another jQuery plugin - <a href="http://malsup.com/jquery/taconite/" title="Taconite jQuery Plugin">Taconite</a>. As the author says: &#8220;The jQuery Taconite Plugin allows you to easily make multiple DOM updates using the results of a single AJAX call. It processes an XML command document that contain instructions for updating the DOM&#8221;. Thanks to this you can for example easily use flash messages in your Ajax views.</p>
<p>Let this be a part of your usual layout:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;flash_notice&quot; class=&quot;flash&quot;&lt;%= ' style=&quot;display: none&quot;' unless flash[:notice] %&gt;&gt;&lt;%= flash[:notice]  %&gt;&lt;/div&gt;</pre></div></div>

<p>Now let this be your taconite layout you&#8217;d use when returning views for Ajax requests:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;taconite&gt;
  &lt;hide select=&quot;#flash_notice&quot; /&gt;
  &lt;% if flash[:notice] %&gt;
    &lt;replaceContent select=&quot;#flash_notice&quot;&gt;
      &lt;%= flash[:notice] %&gt;
    &lt;/replaceContent&gt;
    &lt;fadeIn select=&quot;#flash_notice&quot; arg1=&quot;slow&quot; /&gt;
  &lt;% end %&gt;
  &lt;%= yield %&gt;
&lt;/taconite&gt;</pre></div></div>

<p>This will display flash notice messages with fade-in effect after Ajax requests. Similarly you can update other elements of the page.</p>
<h3>What&#8217;s in your toolbox?</h3>
<p>I would love to hear from you on how you deal with Ajax in your web applications. What libraries/plugins do you use?<br />
<h3>Related posts</h3>
<ul class="related_post">
<li><a href="http://codetunes.com/2009/01/31/rails-222-ajax-and-respond_to/" title="Rails 2.2.2, Ajax and respond_to">Rails 2.2.2, Ajax and respond_to</a></li>
<li><a href="http://codetunes.com/2009/05/09/turning-off-auto-timestamping-for-testing-in-rails/" title="Turning off auto timestamping for testing in Rails">Turning off auto timestamping for testing in Rails</a></li>
<li><a href="http://codetunes.com/2009/03/27/websecurity-through-conventions-and-best-practices/" title="Websecurity through conventions and best practices">Websecurity through conventions and best practices</a></li>
<li><a href="http://codetunes.com/2009/03/23/speaking-at-it-underground-security-conference-and-workshop-in-prague/" title="Speaking at IT Underground Security Conference and Workshop in Prague">Speaking at IT Underground Security Conference and Workshop in Prague</a></li>
<li><a href="http://codetunes.com/2008/07/30/custom-thumbnail-generation-with-paperclip/" title="Custom thumbnail generation with Paperclip">Custom thumbnail generation with Paperclip</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fcodetunes.com%2F2008%2F12%2F08%2Frails-ajax-and-jquery%2F';
  addthis_title  = 'Rails%2C+Ajax+and+jQuery';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>

<p><a href="http://feedads.g.doubleclick.net/~a/7AneY334CEO3wFdM6ayDUNuBB0w/0/da"><img src="http://feedads.g.doubleclick.net/~a/7AneY334CEO3wFdM6ayDUNuBB0w/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/7AneY334CEO3wFdM6ayDUNuBB0w/1/da"><img src="http://feedads.g.doubleclick.net/~a/7AneY334CEO3wFdM6ayDUNuBB0w/1/di" border="0" ismap="true"></img></a></p>]]></content:encoded>
			<wfw:commentRss>http://codetunes.com/2008/12/08/rails-ajax-and-jquery/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
