<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  <title>Railsdog Blog</title>
  <id>http://railsdog.com/blog</id>
  <updated>2011-06-20T18:00:00-04:00</updated>
  <author>
    <name>Sean Schofield</name>
  </author>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/railsdog" /><feedburner:info uri="railsdog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <title>Deploying Spree on Heroku</title>
    <link href="http://feedproxy.google.com/~r/railsdog/~3/zJpuFTkGdg8/" rel="alternate" />
    <id>http://railsdog.com/blog/blog/2011/06/20/deploying-spree-on-heroku/</id>
    <published>2011-06-20T18:00:00-04:00</published>
    <updated>2011-06-20T18:00:00-04:00</updated>
    <author>
      <name>Sean Schofield</name>
    </author>
    <summary type="html">&lt;p&gt;People are often asking us about how to install &lt;a href="http://spreecommerce.com"&gt;Spree&lt;/a&gt; on the popular &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; service.  For those of you who don't know what Heroku is, its a popular cloud-based hosting choice for Rails applications.  The reason why people have a lot of questions about Heroku specifically is due to the unique design of the Heroku platform which presents &lt;a href="http://devcenter.heroku.com/categories/platform-constraints"&gt;a few constraints&lt;/a&gt; not found in a typical Rails hosting environment.&lt;/p&gt;

&lt;h2&gt;Assumptions&lt;/h2&gt;

&lt;p&gt;This is not a how-to guide for Rails applications in general.  So lets start off with a few assumptions about your system.  You can easily Google for some additional information if you do not have the following installed on your system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ruby&lt;/li&gt;
&lt;li&gt;ImageMagick&lt;/li&gt;
&lt;li&gt;Git&lt;/li&gt;
&lt;li&gt;Rubygems&lt;/li&gt;
&lt;li&gt;bundler gem&lt;/li&gt;
&lt;li&gt;heroku gem&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;ImageMagick is not strictly necessary - it is only needed if you plan to upload new pictures for your products.  It is also installed by default on Heroku so it will only be a limitation for you in terms of testing the product image upload locally.&lt;/p&gt;

&lt;h2&gt;Amazon S3 Account&lt;/h2&gt;

&lt;p&gt;The primary limitation of Heroku is that it contains a read-only filesystem.  This means that you cannot simply upload images to the server for your products.  The images need to be stored on a third-party service such as &lt;a href="http://aws.amazon.com/s3/"&gt;Amazon's Simplified Storage System (S3)&lt;/a&gt;.  We will save the details for configuring Spree to work with S3 for a separate blog post.  For now we'll just be satisfied to deploy a basic Spree store.&lt;/p&gt;

&lt;h2&gt;Creating a Basic Spree Application&lt;/h2&gt;

&lt;p&gt;We'll start by creating a basic Rails application.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ rails new spree_demo
$ cd spree_demo
$ bundle install
$ git init
$ git add .
$ git commit -m "First commit"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we'll add the Spree gem by editing the &lt;tt&gt;Gemfile&lt;/tt&gt; in the spree_demo application as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
gem 'spree'
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Once this is done we can install the necessary migrations, etc.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ rails g spree:site
$ rake spree:install
$ rake spree_sample:install
$ rake db:bootstrap
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we'll do another git commit to save our progress.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git add .
$ git commit -m "Added Spree"
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Heroku Code Tweaks&lt;/h2&gt;

&lt;p&gt;Now comes the stuff that is specfic to Heroku.  We'll start by editing &lt;tt&gt;edit config/environments/production.rb&lt;/tt&gt; as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code class='ruby'&gt;
config.action_controller.perform_caching = false
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This tells Rails that the stylesheets and javascript stuff should not be cached in the read-only filesystem (which is not possible on Heroku.)  Once again we perform a git commit to save our progress.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git add .
$ git commit -m "Heroku tweaks"
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Creating the Heroku App&lt;/h2&gt;

&lt;p&gt;Its pretty simple to create a new Heroku application from an existing Rails application already under source control with Git.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE: The following example uses an application name of heroku-spree.  Each Heroku application needs a unique name so you will have to choose your a name that is not already in use.&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ heroku create heroku-spree
Creating heroku-spree....... done
http://heroku-spree.heroku.com/ | git@heroku.com:heroku-spree.git
Git remote heroku added
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we'll deploy the Heroku application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git push heroku master
heroku db:push
heroku restart
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Heroku Configuration&lt;/h2&gt;

&lt;p&gt;Spree requires SSL for checkout in production mode (and this is the default mode for Heroku applications.)  So we'll use the Heroku gem to configure a simple wildcard SSL certificate.  We'll also enable the Sendgrid add-on to send emails.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ heroku addons:add piggyback_ssl
$ heroku addons:add sendgrid:free
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These are free add on services for testing purposes.  You will need your own SSL certificate eventually when you deploy to production.&lt;/p&gt;

&lt;h2&gt;Spree Configuration&lt;/h2&gt;

&lt;p&gt;You should now be up and running on Heroku!  The final step is to configure Spree itself so that you can do a complete checkout.  To do this, just log into the admin interface and create a production payment method that uses the &lt;tt&gt;Gateway::Bogus&lt;/tt&gt; gateway.  For more information on configuring a payment method see the &lt;a href="http://spreecommerce.com/documentation"&gt;official Spree documentation&lt;/a&gt;.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;People are often asking us about how to install &lt;a href="http://spreecommerce.com"&gt;Spree&lt;/a&gt; on the popular &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; service.  For those of you who don't know what Heroku is, its a popular cloud-based hosting choice for Rails applications.  The reason why people have a lot of questions about Heroku specifically is due to the unique design of the Heroku platform which presents &lt;a href="http://devcenter.heroku.com/categories/platform-constraints"&gt;a few constraints&lt;/a&gt; not found in a typical Rails hosting environment.&lt;/p&gt;

&lt;h2&gt;Assumptions&lt;/h2&gt;

&lt;p&gt;This is not a how-to guide for Rails applications in general.  So lets start off with a few assumptions about your system.  You can easily Google for some additional information if you do not have the following installed on your system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ruby&lt;/li&gt;
&lt;li&gt;ImageMagick&lt;/li&gt;
&lt;li&gt;Git&lt;/li&gt;
&lt;li&gt;Rubygems&lt;/li&gt;
&lt;li&gt;bundler gem&lt;/li&gt;
&lt;li&gt;heroku gem&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;ImageMagick is not strictly necessary - it is only needed if you plan to upload new pictures for your products.  It is also installed by default on Heroku so it will only be a limitation for you in terms of testing the product image upload locally.&lt;/p&gt;

&lt;h2&gt;Amazon S3 Account&lt;/h2&gt;

&lt;p&gt;The primary limitation of Heroku is that it contains a read-only filesystem.  This means that you cannot simply upload images to the server for your products.  The images need to be stored on a third-party service such as &lt;a href="http://aws.amazon.com/s3/"&gt;Amazon's Simplified Storage System (S3)&lt;/a&gt;.  We will save the details for configuring Spree to work with S3 for a separate blog post.  For now we'll just be satisfied to deploy a basic Spree store.&lt;/p&gt;

&lt;h2&gt;Creating a Basic Spree Application&lt;/h2&gt;

&lt;p&gt;We'll start by creating a basic Rails application.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ rails new spree_demo
$ cd spree_demo
$ bundle install
$ git init
$ git add .
$ git commit -m "First commit"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we'll add the Spree gem by editing the &lt;tt&gt;Gemfile&lt;/tt&gt; in the spree_demo application as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
gem 'spree'
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Once this is done we can install the necessary migrations, etc.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ rails g spree:site
$ rake spree:install
$ rake spree_sample:install
$ rake db:bootstrap
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we'll do another git commit to save our progress.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git add .
$ git commit -m "Added Spree"
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Heroku Code Tweaks&lt;/h2&gt;

&lt;p&gt;Now comes the stuff that is specfic to Heroku.  We'll start by editing &lt;tt&gt;edit config/environments/production.rb&lt;/tt&gt; as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code class='ruby'&gt;
config.action_controller.perform_caching = false
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This tells Rails that the stylesheets and javascript stuff should not be cached in the read-only filesystem (which is not possible on Heroku.)  Once again we perform a git commit to save our progress.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git add .
$ git commit -m "Heroku tweaks"
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Creating the Heroku App&lt;/h2&gt;

&lt;p&gt;Its pretty simple to create a new Heroku application from an existing Rails application already under source control with Git.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE: The following example uses an application name of heroku-spree.  Each Heroku application needs a unique name so you will have to choose your a name that is not already in use.&lt;/em&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ heroku create heroku-spree
Creating heroku-spree....... done
http://heroku-spree.heroku.com/ | git@heroku.com:heroku-spree.git
Git remote heroku added
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we'll deploy the Heroku application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git push heroku master
heroku db:push
heroku restart
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Heroku Configuration&lt;/h2&gt;

&lt;p&gt;Spree requires SSL for checkout in production mode (and this is the default mode for Heroku applications.)  So we'll use the Heroku gem to configure a simple wildcard SSL certificate.  We'll also enable the Sendgrid add-on to send emails.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ heroku addons:add piggyback_ssl
$ heroku addons:add sendgrid:free
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These are free add on services for testing purposes.  You will need your own SSL certificate eventually when you deploy to production.&lt;/p&gt;

&lt;h2&gt;Spree Configuration&lt;/h2&gt;

&lt;p&gt;You should now be up and running on Heroku!  The final step is to configure Spree itself so that you can do a complete checkout.  To do this, just log into the admin interface and create a production payment method that uses the &lt;tt&gt;Gateway::Bogus&lt;/tt&gt; gateway.  For more information on configuring a payment method see the &lt;a href="http://spreecommerce.com/documentation"&gt;official Spree documentation&lt;/a&gt;.&lt;/p&gt;
</content>
  <feedburner:origLink>http://railsdog.com/blog/blog/2011/06/20/deploying-spree-on-heroku/</feedburner:origLink></entry>
  <entry>
    <title>Adding filter_parameters in an Engine</title>
    <link href="http://feedproxy.google.com/~r/railsdog/~3/LQ9WFDlE39I/" rel="alternate" />
    <id>http://railsdog.com/blog/blog/2011/06/02/adding-filter-parameters-in-an-engine/</id>
    <published>2011-06-04T18:00:00-04:00</published>
    <updated>2011-06-04T18:00:00-04:00</updated>
    <author>
      <name>Sean Schofield</name>
    </author>
    <summary type="html">&lt;p&gt;Rails applications by default will log every parameter that is passed to a given controller.  Normally this is desirable behavior but in the case of sensitive information (ex. passwords and credit card numbers) you should never log these values.&lt;/p&gt;

&lt;p&gt;The recommended approach for this is to add a &lt;tt&gt;filter_parameters&lt;/tt&gt; directive in your application configuration as shown below.&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
module SampleApp
  class Application &lt; Rails::Application
    # Filter sensitive parameters from the log file.
    config.filter_parameters += [:password]
  end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;But what if you are working within the context of a &lt;a href="http://edgeapi.rubyonrails.org/classes/Rails/Engine.html"&gt;Rails Engine&lt;/a&gt;?  For instance, in the &lt;a href="http://spreecommerce"&gt;Spree&lt;/a&gt; application there is an engine that has a controller responsible for posting credit card information (over SSL of course.)  After a little bit of digging I came up with the following solution:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
module SpreeCore
  class Engine &lt; Rails::Engine
    # filter sensitive information during logging
    initializer "spree.params.filter" do |app|
      app.config.filter_parameters += [:number]
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;It turns out you can dynamically declare an initializer in your Railtie and then just add the filter there.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Rails applications by default will log every parameter that is passed to a given controller.  Normally this is desirable behavior but in the case of sensitive information (ex. passwords and credit card numbers) you should never log these values.&lt;/p&gt;

&lt;p&gt;The recommended approach for this is to add a &lt;tt&gt;filter_parameters&lt;/tt&gt; directive in your application configuration as shown below.&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
module SampleApp
  class Application &lt; Rails::Application
    # Filter sensitive parameters from the log file.
    config.filter_parameters += [:password]
  end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;But what if you are working within the context of a &lt;a href="http://edgeapi.rubyonrails.org/classes/Rails/Engine.html"&gt;Rails Engine&lt;/a&gt;?  For instance, in the &lt;a href="http://spreecommerce"&gt;Spree&lt;/a&gt; application there is an engine that has a controller responsible for posting credit card information (over SSL of course.)  After a little bit of digging I came up with the following solution:&lt;/p&gt;

&lt;pre&gt;&lt;code class="ruby"&gt;
module SpreeCore
  class Engine &lt; Rails::Engine
    # filter sensitive information during logging
    initializer "spree.params.filter" do |app|
      app.config.filter_parameters += [:number]
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;It turns out you can dynamically declare an initializer in your Railtie and then just add the filter there.&lt;/p&gt;
</content>
  <feedburner:origLink>http://railsdog.com/blog/blog/2011/06/02/adding-filter-parameters-in-an-engine/</feedburner:origLink></entry>
  <entry>
    <title>Callbacks in jQuery-ujs in Rails 3</title>
    <link href="http://feedproxy.google.com/~r/railsdog/~3/ItDMHqRBLT0/" rel="alternate" />
    <id>http://railsdog.com/blog/blog/2011/02/28/callbacks-in-jquery-ujs-in-rails3/</id>
    <published>2011-02-28T17:00:00-05:00</published>
    <updated>2011-02-28T17:00:00-05:00</updated>
    <author>
      <name>Neeraj Singh</name>
    </author>
    <summary type="html">&lt;h2&gt;Deleting a record using ajax&lt;/h2&gt;

&lt;p&gt;The rails scaffold produces following code for deleting a record.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
&lt;%= link_to 'Destroy', user,  :confirm =&gt; 'Are you sure?',
                              :method =&gt; :delete %&gt;
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;I want to delete the record using Ajax so that the whole page is not reloaded after every single delete. Since I use jQuery, I would add following line to my Gemfile.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
gem 'jquery-rails', '&gt;= 0.2.6'
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;And then I would run&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
rails generate jquery:install
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now I am all set. Howeer if I click "Destroy" then record is getting destroyed but the browser version is not being removed. Let's fix that.&lt;/p&gt;

&lt;p&gt;First thing I need to do is to respond_to "ajax" requets in destroy action. After changing the controller the method looks like this.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
def destroy
  @user = User.find(params[:id])
  @user.destroy
  respond_to do |format|
    format.html { redirect_to(users_url) }
    format.js { render :text =&gt; "alert('user has been deleted')" }
  end
end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;And in application.js I would add just one line.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="javascript"&gt;
$('a[data-method="delete"]').live('ajax:success', function(){});
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Then index.html.erb will look like this.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
&lt;%= link_to 'Destroy', user,  :confirm =&gt; 'Are you sure?',
                              :method =&gt; :delete,
                              :remote =&gt; true
%&gt;
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now when you delete a user, the user record will be deleted using ajax and an alert will come up with the message &lt;tt&gt;user has been deleted&lt;/tt&gt;.&lt;/p&gt;

&lt;h2&gt;Deleting record using JSON data-type&lt;/h2&gt;

&lt;p&gt;JSON is the currency of communication in JavaScript world. So let's say that I want ajax request to respond with the name of the user who is being deleted and then I would construct the message on the client. Changed code would look like this.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    def destroy
      @user = User.find(params[:id])
      @user.destroy
      respond_to do |format|
        format.html { redirect_to(users_url) }
        format.js { render :json =&gt; {:name =&gt; 'John'} }
      end
    end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;And in application.js I would have following lines.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="javascript"&gt;
$('a[data-method="delete"]').live('ajax:success',
  function(data, textStatus, jqXHR){
    alert(data.name + ' has been deleted');
  });
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Then index.html.erb will look like this.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
&lt;%= link_to 'Destroy', user,  :confirm =&gt; 'Are you sure?',
                              :method =&gt; :delete,
                              :remote =&gt; true,
                              'data-type' =&gt; 'json' %&gt;
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now when you delete a user, you will get the message &lt;tt&gt;undefined has been deleted&lt;/tt&gt;. What's going wrong.&lt;/p&gt;

&lt;p&gt;Notice that the function that is invoked when &lt;tt&gt;ajax:success&lt;/tt&gt; callbacks happen has three parameters. The order of the parameters matches with the order of the paramers in &lt;a href="http://api.jquery.com/jQuery.ajax"&gt;jQuery ajax call&lt;/a&gt; for success.&lt;/p&gt;

&lt;p&gt;In order to solve the problem you need to know how rails.js invokes the callbacks. The relevant code in rails.js is&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="javascript"&gt;
element.trigger('ajax:success', [data, status, xhr]);
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;When trigger is used then the very first parameter passed is the event object. The function that is invoked will get the event object as the first parameter and then rest of the parameters are passed. This is just the way trigger works in jQuery.&lt;/p&gt;

&lt;p&gt;Based on that information the JavaScript code should be changed to following in &lt;tt&gt;application.js&lt;/tt&gt; .&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="javascript"&gt;
$('a[data-method="delete"]').live('ajax:success',
  function(e, data, textStatus, jqXHR){
    alert(data.name + ' has been deleted');
  }
);
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;You can &lt;a href="https://github.com/rails/jquery-ujs/pull/115"&gt;follow this issue&lt;/a&gt; to see a real world example of this topic.&lt;/p&gt;
</summary>
    <content type="html">&lt;h2&gt;Deleting a record using ajax&lt;/h2&gt;

&lt;p&gt;The rails scaffold produces following code for deleting a record.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
&lt;%= link_to 'Destroy', user,  :confirm =&gt; 'Are you sure?',
                              :method =&gt; :delete %&gt;
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;I want to delete the record using Ajax so that the whole page is not reloaded after every single delete. Since I use jQuery, I would add following line to my Gemfile.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
gem 'jquery-rails', '&gt;= 0.2.6'
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;And then I would run&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
rails generate jquery:install
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now I am all set. Howeer if I click "Destroy" then record is getting destroyed but the browser version is not being removed. Let's fix that.&lt;/p&gt;

&lt;p&gt;First thing I need to do is to respond_to "ajax" requets in destroy action. After changing the controller the method looks like this.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
def destroy
  @user = User.find(params[:id])
  @user.destroy
  respond_to do |format|
    format.html { redirect_to(users_url) }
    format.js { render :text =&gt; "alert('user has been deleted')" }
  end
end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;And in application.js I would add just one line.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="javascript"&gt;
$('a[data-method="delete"]').live('ajax:success', function(){});
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Then index.html.erb will look like this.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
&lt;%= link_to 'Destroy', user,  :confirm =&gt; 'Are you sure?',
                              :method =&gt; :delete,
                              :remote =&gt; true
%&gt;
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now when you delete a user, the user record will be deleted using ajax and an alert will come up with the message &lt;tt&gt;user has been deleted&lt;/tt&gt;.&lt;/p&gt;

&lt;h2&gt;Deleting record using JSON data-type&lt;/h2&gt;

&lt;p&gt;JSON is the currency of communication in JavaScript world. So let's say that I want ajax request to respond with the name of the user who is being deleted and then I would construct the message on the client. Changed code would look like this.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    def destroy
      @user = User.find(params[:id])
      @user.destroy
      respond_to do |format|
        format.html { redirect_to(users_url) }
        format.js { render :json =&gt; {:name =&gt; 'John'} }
      end
    end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;And in application.js I would have following lines.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="javascript"&gt;
$('a[data-method="delete"]').live('ajax:success',
  function(data, textStatus, jqXHR){
    alert(data.name + ' has been deleted');
  });
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Then index.html.erb will look like this.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
&lt;%= link_to 'Destroy', user,  :confirm =&gt; 'Are you sure?',
                              :method =&gt; :delete,
                              :remote =&gt; true,
                              'data-type' =&gt; 'json' %&gt;
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now when you delete a user, you will get the message &lt;tt&gt;undefined has been deleted&lt;/tt&gt;. What's going wrong.&lt;/p&gt;

&lt;p&gt;Notice that the function that is invoked when &lt;tt&gt;ajax:success&lt;/tt&gt; callbacks happen has three parameters. The order of the parameters matches with the order of the paramers in &lt;a href="http://api.jquery.com/jQuery.ajax"&gt;jQuery ajax call&lt;/a&gt; for success.&lt;/p&gt;

&lt;p&gt;In order to solve the problem you need to know how rails.js invokes the callbacks. The relevant code in rails.js is&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="javascript"&gt;
element.trigger('ajax:success', [data, status, xhr]);
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;When trigger is used then the very first parameter passed is the event object. The function that is invoked will get the event object as the first parameter and then rest of the parameters are passed. This is just the way trigger works in jQuery.&lt;/p&gt;

&lt;p&gt;Based on that information the JavaScript code should be changed to following in &lt;tt&gt;application.js&lt;/tt&gt; .&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="javascript"&gt;
$('a[data-method="delete"]').live('ajax:success',
  function(e, data, textStatus, jqXHR){
    alert(data.name + ' has been deleted');
  }
);
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;You can &lt;a href="https://github.com/rails/jquery-ujs/pull/115"&gt;follow this issue&lt;/a&gt; to see a real world example of this topic.&lt;/p&gt;
</content>
  <feedburner:origLink>http://railsdog.com/blog/blog/2011/02/28/callbacks-in-jquery-ujs-in-rails3/</feedburner:origLink></entry>
  <entry>
    <title>Cucumber Testing Tips</title>
    <link href="http://feedproxy.google.com/~r/railsdog/~3/AvexslGZ2z0/" rel="alternate" />
    <id>http://railsdog.com/blog/blog/2011/02/22/cucumber-testing-tips/</id>
    <published>2011-02-22T17:00:00-05:00</published>
    <updated>2011-02-22T17:00:00-05:00</updated>
    <author>
      <name>Neeraj Singh</name>
    </author>
    <summary type="html">&lt;p&gt;Here are some of the testing related tips I use while working on &lt;a href="http://spreecommerce.com"&gt;Spree&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Stop in rails console if something fails in cucumber test&lt;/h2&gt;

&lt;p&gt;If a cucumber test fails then one of the first things I check for is the state of current data. In order to do that I used to put &lt;tt&gt;debugger&lt;/tt&gt; statements. I still use this technique but here is a much better approach.&lt;/p&gt;

&lt;p&gt;Create a file called &lt;tt&gt;stop.rb&lt;/tt&gt; and put this file at &lt;tt&gt;features/support/stop.rb&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;The contents of &lt;tt&gt;stop.rb&lt;/tt&gt; are as follows:&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    # Usage:
    #  @wip @stop
    #  Scenario: change password
    #    ........................
    # $ cucumber -p wip
    After do |scenario|
      if scenario.failed? &amp;&amp; scenario.source_tag_names.include?("@wip") &amp;&amp; scenario.source_tag_names.include?("@stop")
        puts "Scenario failed. You are in rails console becuase of @stop. Type exit when you are done"
        require 'irb'
        require 'irb/completion'
        ARGV.clear
        IRB.start
      end
    end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;At the end of &lt;tt&gt;features/support/env.rb&lt;/tt&gt; add the following line&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    require File.expand_path(File.dirname(__FILE__) + '/stop')
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;As mentioned in the &lt;tt&gt;Usage&lt;/tt&gt; above, add tags &lt;tt&gt;@wip&lt;/tt&gt; and &lt;tt&gt;@stop&lt;/tt&gt; to a scenario. Now execute the cucumber tests using following command.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    $ cucumber -p wip
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If your cucumber test fails then you will be taken to a rails console where you can do things like&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    Scenario failed. You are in rails console becuase of @stop. Type exit when you are done
    ree-1.8.7-2010.02 :001 &gt; p User.count
      SQL (0.2ms)  SELECT COUNT(*) FROM "users"
      1
    =&gt; nil
  &lt;/code&gt;
&lt;/pre&gt;


&lt;h2&gt;Running a single cucumber feature&lt;/h2&gt;

&lt;p&gt;If you want to execute only a single cucumber feature then the syntax for that is&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    $ bundle exec cucumber features/products.feature --require features
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Typing that long command could be tedious. This is what I do&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    $ c features/products.feature
    $ bundle exec cucumber features/products.feature --require features
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;I typed the first line and the second line is what is actually executed. I have following lines in my &lt;tt&gt;~/.bashrc&lt;/tt&gt;.&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    function c () {
      if [ $# -eq 0 ]; then
        local cmd="bundle exec cucumber"
      else
        local cmd="bundle exec cucumber $@ --require features"
      fi
      echo $cmd
      eval $cmd
    }
  &lt;/code&gt;
&lt;/pre&gt;



</summary>
    <content type="html">&lt;p&gt;Here are some of the testing related tips I use while working on &lt;a href="http://spreecommerce.com"&gt;Spree&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Stop in rails console if something fails in cucumber test&lt;/h2&gt;

&lt;p&gt;If a cucumber test fails then one of the first things I check for is the state of current data. In order to do that I used to put &lt;tt&gt;debugger&lt;/tt&gt; statements. I still use this technique but here is a much better approach.&lt;/p&gt;

&lt;p&gt;Create a file called &lt;tt&gt;stop.rb&lt;/tt&gt; and put this file at &lt;tt&gt;features/support/stop.rb&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;The contents of &lt;tt&gt;stop.rb&lt;/tt&gt; are as follows:&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    # Usage:
    #  @wip @stop
    #  Scenario: change password
    #    ........................
    # $ cucumber -p wip
    After do |scenario|
      if scenario.failed? &amp;&amp; scenario.source_tag_names.include?("@wip") &amp;&amp; scenario.source_tag_names.include?("@stop")
        puts "Scenario failed. You are in rails console becuase of @stop. Type exit when you are done"
        require 'irb'
        require 'irb/completion'
        ARGV.clear
        IRB.start
      end
    end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;At the end of &lt;tt&gt;features/support/env.rb&lt;/tt&gt; add the following line&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    require File.expand_path(File.dirname(__FILE__) + '/stop')
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;As mentioned in the &lt;tt&gt;Usage&lt;/tt&gt; above, add tags &lt;tt&gt;@wip&lt;/tt&gt; and &lt;tt&gt;@stop&lt;/tt&gt; to a scenario. Now execute the cucumber tests using following command.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    $ cucumber -p wip
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If your cucumber test fails then you will be taken to a rails console where you can do things like&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    Scenario failed. You are in rails console becuase of @stop. Type exit when you are done
    ree-1.8.7-2010.02 :001 &gt; p User.count
      SQL (0.2ms)  SELECT COUNT(*) FROM "users"
      1
    =&gt; nil
  &lt;/code&gt;
&lt;/pre&gt;


&lt;h2&gt;Running a single cucumber feature&lt;/h2&gt;

&lt;p&gt;If you want to execute only a single cucumber feature then the syntax for that is&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    $ bundle exec cucumber features/products.feature --require features
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Typing that long command could be tedious. This is what I do&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    $ c features/products.feature
    $ bundle exec cucumber features/products.feature --require features
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;I typed the first line and the second line is what is actually executed. I have following lines in my &lt;tt&gt;~/.bashrc&lt;/tt&gt;.&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    function c () {
      if [ $# -eq 0 ]; then
        local cmd="bundle exec cucumber"
      else
        local cmd="bundle exec cucumber $@ --require features"
      fi
      echo $cmd
      eval $cmd
    }
  &lt;/code&gt;
&lt;/pre&gt;



</content>
  <feedburner:origLink>http://railsdog.com/blog/blog/2011/02/22/cucumber-testing-tips/</feedburner:origLink></entry>
  <entry>
    <title>Do Not Require Using Expanded Path</title>
    <link href="http://feedproxy.google.com/~r/railsdog/~3/9Y2mYP2-Ghc/" rel="alternate" />
    <id>http://railsdog.com/blog/blog/2011/01/25/donot-require-using-expanded-path/</id>
    <published>2011-01-25T17:00:00-05:00</published>
    <updated>2011-01-25T17:00:00-05:00</updated>
    <author>
      <name>Neeraj Singh</name>
    </author>
    <summary type="html">&lt;p&gt;My &lt;a href="/blog/2011/01/19/require-using-expanded-path/"&gt;previous blog post&lt;/a&gt; received some good comments. In this blog post I am going to further expand on what others are saying .i.e. using expanded path is a bad idea.&lt;/p&gt;

&lt;h2&gt;Ability to provide alternative implementation&lt;/h2&gt;

&lt;p&gt;Let's say I have two files.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #main.rb
    require 'lib/util'

    #lib/util.rb
    puts "Hello World"
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If you execute &lt;tt&gt;ruby main.rb&lt;/tt&gt; you will see &lt;tt&gt;Hello World&lt;/tt&gt;. No surprise there.&lt;/p&gt;

&lt;p&gt;However let's say that I have another directory called &lt;tt&gt;common&lt;/tt&gt; in my dev folder. In this dirctory create a directory called &lt;tt&gt;lib&lt;/tt&gt; and then store a file named &lt;tt&gt;util.rb&lt;/tt&gt;.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #~/dev/common/lib/util.rb
    puts "Hello Common World"
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now let's say that for testing purpose I want &lt;tt&gt;main.rb&lt;/tt&gt; to load &lt;tt&gt;util.rb&lt;/tt&gt; from our &lt;tt&gt;common&lt;/tt&gt; library. Here is how you have to do to ensure that &lt;tt&gt;util.rb&lt;/tt&gt; is picked up from &lt;tt&gt;common&lt;/tt&gt; directory.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    ruby -I/Users/nsingh/dev/common main.rb
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;This time the output I got was&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    Hello Common World
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now let's change original &lt;tt&gt;main.rb&lt;/tt&gt; to be use expanded_path. New code would look like this&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    require File.expand_path("../lib/util", __FILE__)
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now let's try to use &lt;tt&gt;util.rb&lt;/tt&gt; from &lt;tt&gt;common&lt;/tt&gt; directory.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    ruby -I/Users/nsingh/dev/common main.rb
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;This time output will be NOT be what we saw last time.&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    Hello World
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;This is exactly what &lt;a href="http://www.ruby-forum.com/topic/749955#970994"&gt;Aaraon Patterson&lt;/a&gt; is talking about when he says if you use &lt;tt&gt;File.expand_path&lt;/tt&gt; then others can't provide an alternative implementation.&lt;/p&gt;

&lt;p&gt;In real life I have never done anything like providing alternative implemenation of an item. However I do understand that in theory it makes sense to not to use expanded path.&lt;/p&gt;

&lt;h2&gt;spec_helper&lt;/h2&gt;

&lt;p&gt;If you are testing using rspec then your specs will have &lt;tt&gt;require 'spec_helper'&lt;/tt&gt; at the top of the file. If you want to run tests for an individual file then you can do&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    ruby spec/models/user_spec.rb
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;&lt;strong&gt;Note that there are better ways to execute a particular rspec file. I am using the given case only to discuss about require using relative path vs require using absolute path.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;However that code will fail since &lt;tt&gt;spec_helper&lt;/tt&gt; is not in the path. One way to fix that would be to execute following command&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    ruby -Ispec spec/models/user_spec.rb
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Another way to fix it would be to change the &lt;tt&gt;user_spec.rb&lt;/tt&gt; to require &lt;tt&gt;spec_heper&lt;/tt&gt; using expanded path.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    require File.dirname(__FILE__) + '/../spec_helper'
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now you can run &lt;tt&gt;ruby spec/models/user_spec.rb&lt;/tt&gt; and it will run fine. However declaring expanded path comes with a cost. Let's say that tomorrow I move &lt;tt&gt;user_spec.rb&lt;/tt&gt; to &lt;tt&gt;spec/models/users&lt;/tt&gt; directory. Now the expanded path is pointing to a wrong file. If I had used &lt;tt&gt;spec_helper.rb&lt;/tt&gt; then this problem would have not been there.&lt;/p&gt;

&lt;p&gt;Since usage of expanded path reduces the flexibility of providing alternative implementation and increases the cost of moving around files, I am changing my vote from using expanded path to not using expanded path.&lt;/p&gt;

&lt;h2&gt;requiring rubygems&lt;/h2&gt;

&lt;p&gt;Ryan Tomayko wrote about the need to &lt;a href="https://gist.github.com/54177"&gt;not to require&lt;/a&gt; . Sintarta testing &lt;a href="http://www.sinatrarb.com/testing.html"&gt;documentation&lt;/a&gt; follows that guidelines and does not require &lt;tt&gt;rubygems&lt;/tt&gt;. Here is the issue. If a ruby newbie comes along and stores the test file in &lt;tt&gt;test.rb&lt;/tt&gt; and executes the file with command &lt;tt&gt;ruby test.rb &lt;/tt&gt;, then the user will get following error message&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    `require': no such file to load 'sinatra'
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Most likey user will do &lt;tt&gt;gem install sinatra&lt;/tt&gt; and it will still not work. The user does not know that &lt;tt&gt;rubygems&lt;/tt&gt; is not being loaded. The correct command would be&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    ruby -rubygems test.rb
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;I like the approach taken by cucumber. If you look at &lt;tt&gt;cucumber/cli/main.rb&lt;/tt&gt; you will see following code&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    begin
      require 'gherkin'
    rescue LoadError
      require 'rubygems'
      require 'gherkin'
    end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;The cucumber team is trying to not to require &lt;tt&gt;rubygems&lt;/tt&gt; but in case rubygems is not already loaded then they load it for you.&lt;/p&gt;

&lt;p&gt;I know this rubygems issue is not directly related to expanded path discussion but since it came in the discussion in the comments, I wanted to provide my point of view.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;My &lt;a href="/blog/2011/01/19/require-using-expanded-path/"&gt;previous blog post&lt;/a&gt; received some good comments. In this blog post I am going to further expand on what others are saying .i.e. using expanded path is a bad idea.&lt;/p&gt;

&lt;h2&gt;Ability to provide alternative implementation&lt;/h2&gt;

&lt;p&gt;Let's say I have two files.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #main.rb
    require 'lib/util'

    #lib/util.rb
    puts "Hello World"
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If you execute &lt;tt&gt;ruby main.rb&lt;/tt&gt; you will see &lt;tt&gt;Hello World&lt;/tt&gt;. No surprise there.&lt;/p&gt;

&lt;p&gt;However let's say that I have another directory called &lt;tt&gt;common&lt;/tt&gt; in my dev folder. In this dirctory create a directory called &lt;tt&gt;lib&lt;/tt&gt; and then store a file named &lt;tt&gt;util.rb&lt;/tt&gt;.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #~/dev/common/lib/util.rb
    puts "Hello Common World"
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now let's say that for testing purpose I want &lt;tt&gt;main.rb&lt;/tt&gt; to load &lt;tt&gt;util.rb&lt;/tt&gt; from our &lt;tt&gt;common&lt;/tt&gt; library. Here is how you have to do to ensure that &lt;tt&gt;util.rb&lt;/tt&gt; is picked up from &lt;tt&gt;common&lt;/tt&gt; directory.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    ruby -I/Users/nsingh/dev/common main.rb
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;This time the output I got was&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    Hello Common World
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now let's change original &lt;tt&gt;main.rb&lt;/tt&gt; to be use expanded_path. New code would look like this&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    require File.expand_path("../lib/util", __FILE__)
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now let's try to use &lt;tt&gt;util.rb&lt;/tt&gt; from &lt;tt&gt;common&lt;/tt&gt; directory.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    ruby -I/Users/nsingh/dev/common main.rb
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;This time output will be NOT be what we saw last time.&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    Hello World
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;This is exactly what &lt;a href="http://www.ruby-forum.com/topic/749955#970994"&gt;Aaraon Patterson&lt;/a&gt; is talking about when he says if you use &lt;tt&gt;File.expand_path&lt;/tt&gt; then others can't provide an alternative implementation.&lt;/p&gt;

&lt;p&gt;In real life I have never done anything like providing alternative implemenation of an item. However I do understand that in theory it makes sense to not to use expanded path.&lt;/p&gt;

&lt;h2&gt;spec_helper&lt;/h2&gt;

&lt;p&gt;If you are testing using rspec then your specs will have &lt;tt&gt;require 'spec_helper'&lt;/tt&gt; at the top of the file. If you want to run tests for an individual file then you can do&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    ruby spec/models/user_spec.rb
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;&lt;strong&gt;Note that there are better ways to execute a particular rspec file. I am using the given case only to discuss about require using relative path vs require using absolute path.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;However that code will fail since &lt;tt&gt;spec_helper&lt;/tt&gt; is not in the path. One way to fix that would be to execute following command&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    ruby -Ispec spec/models/user_spec.rb
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Another way to fix it would be to change the &lt;tt&gt;user_spec.rb&lt;/tt&gt; to require &lt;tt&gt;spec_heper&lt;/tt&gt; using expanded path.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    require File.dirname(__FILE__) + '/../spec_helper'
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Now you can run &lt;tt&gt;ruby spec/models/user_spec.rb&lt;/tt&gt; and it will run fine. However declaring expanded path comes with a cost. Let's say that tomorrow I move &lt;tt&gt;user_spec.rb&lt;/tt&gt; to &lt;tt&gt;spec/models/users&lt;/tt&gt; directory. Now the expanded path is pointing to a wrong file. If I had used &lt;tt&gt;spec_helper.rb&lt;/tt&gt; then this problem would have not been there.&lt;/p&gt;

&lt;p&gt;Since usage of expanded path reduces the flexibility of providing alternative implementation and increases the cost of moving around files, I am changing my vote from using expanded path to not using expanded path.&lt;/p&gt;

&lt;h2&gt;requiring rubygems&lt;/h2&gt;

&lt;p&gt;Ryan Tomayko wrote about the need to &lt;a href="https://gist.github.com/54177"&gt;not to require&lt;/a&gt; . Sintarta testing &lt;a href="http://www.sinatrarb.com/testing.html"&gt;documentation&lt;/a&gt; follows that guidelines and does not require &lt;tt&gt;rubygems&lt;/tt&gt;. Here is the issue. If a ruby newbie comes along and stores the test file in &lt;tt&gt;test.rb&lt;/tt&gt; and executes the file with command &lt;tt&gt;ruby test.rb &lt;/tt&gt;, then the user will get following error message&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    `require': no such file to load 'sinatra'
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Most likey user will do &lt;tt&gt;gem install sinatra&lt;/tt&gt; and it will still not work. The user does not know that &lt;tt&gt;rubygems&lt;/tt&gt; is not being loaded. The correct command would be&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    ruby -rubygems test.rb
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;I like the approach taken by cucumber. If you look at &lt;tt&gt;cucumber/cli/main.rb&lt;/tt&gt; you will see following code&lt;/p&gt;

&lt;pre&gt;
  &lt;code class='ruby'&gt;
    begin
      require 'gherkin'
    rescue LoadError
      require 'rubygems'
      require 'gherkin'
    end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;The cucumber team is trying to not to require &lt;tt&gt;rubygems&lt;/tt&gt; but in case rubygems is not already loaded then they load it for you.&lt;/p&gt;

&lt;p&gt;I know this rubygems issue is not directly related to expanded path discussion but since it came in the discussion in the comments, I wanted to provide my point of view.&lt;/p&gt;
</content>
  <feedburner:origLink>http://railsdog.com/blog/blog/2011/01/25/donot-require-using-expanded-path/</feedburner:origLink></entry>
  <entry>
    <title>Require Using Expanded Path</title>
    <link href="http://feedproxy.google.com/~r/railsdog/~3/7HNCXaw72ks/" rel="alternate" />
    <id>http://railsdog.com/blog/blog/2011/01/19/require-using-expanded-path/</id>
    <published>2010-01-19T17:00:00-05:00</published>
    <updated>2010-01-19T17:00:00-05:00</updated>
    <author>
      <name>Neeraj Singh</name>
    </author>
    <summary type="html">&lt;p&gt;The require command is not suppossed to load a file that is already loaded. However while requiring a file, ruby VM looks at the path and the file name and it can cause some strange behavior.&lt;/p&gt;

&lt;h2&gt;Basics&lt;/h2&gt;

&lt;p&gt;Ruby allows two ways to load file: require and load. &lt;tt&gt;load&lt;/tt&gt; will load a file every single time while require will not load already loaded file.&lt;/p&gt;

&lt;p&gt;I have two files here in the same directory: &lt;tt&gt;main.rb&lt;/tt&gt; and &lt;tt&gt;foo.rb&lt;/tt&gt; . Here is what files look like.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #main.rb
    require 'foo'
    require 'foo'
  &lt;/code&gt;
&lt;/pre&gt;




&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #foo.rb
    puts "I am being loaded"
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If you execute &lt;tt&gt;ruby main.rb&lt;/tt&gt; you will see a single message "I am being loaded". That is because once a file is already loaded then it wil not be loaded second time.&lt;/p&gt;

&lt;h2&gt;Require using relative path&lt;/h2&gt;

&lt;p&gt;Now let's change the &lt;tt&gt;main.rb&lt;/tt&gt; to require &lt;tt&gt;foo.rb&lt;/tt&gt; using relative path. After the change file looks like this&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #main.rb
    require 'foo'
    require '../dev/foo'
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If you execute &lt;tt&gt;ruby main.rb&lt;/tt&gt; you will see the puts message twice. That is because ruby is loading &lt;tt&gt;foo.rb&lt;/tt&gt; twice.&lt;/p&gt;

&lt;h2&gt;$LOADED_FEATURES - keeping track of what's loaded and what's not&lt;/h2&gt;

&lt;p&gt;ruby uses $LOADED_FEATURES , a global constant, to keep track of what's loaded and what's not loaded. Here is an updated version of &lt;tt&gt;main.rb&lt;/tt&gt;.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #main.rb
    require 'foo'
    require 'foo'
    puts $LOADED_FEATURES.inspect
    require '../dev/foo'
    puts $LOADED_FEATURES.inspect
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If you execute &lt;tt&gt;ruby main.rb&lt;/tt&gt; you will see following message.&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    I am being loaded
    ["enumerator.so", "foo.rb"]
    I am being loaded
    ["enumerator.so", "foo.rb", "../dev/foo.rb"]
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;As you can see ruby while loading a file also stores the path that was provided. Now that we know that ruby looks at the path fix is easy. Here  is fixed version.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    require File.expand_path('../foo', __FILE__)
    puts $LOADED_FEATURES.inspect
    require File.expand_path('../../lab/foo', __FILE__)
    puts $LOADED_FEATURES.inspect
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If you execute &lt;tt&gt;ruby main.rb&lt;/tt&gt; you will see following message.&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    I am being loaded
    ["enumerator.so", "/Users/nsingh/dev/scratch/lab/foo.rb"]
    ["enumerator.so", "/Users/nsingh/dev/scratch/lab/foo.rb"]
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;As you can see &lt;tt&gt;foo.rb&lt;/tt&gt; is being loaded only once and that is because &lt;tt&gt;$LOADED_FEATURES&lt;/tt&gt; has the absolute path to &lt;tt&gt;foo.rb&lt;/tt&gt;.&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;There is no need to have abosolute path in all the cases. However in a complex case where a number of different files are being required and some of the required files in turn require more files dynamically then it is best to have a policy of everyone requiring file using absolute path.&lt;/p&gt;

&lt;p&gt;Recently in the &lt;a href="http://spreecommerce.com"&gt;Spree&lt;/a&gt; project, the tests started to fail with the message that &lt;tt&gt;factories are already loaded&lt;/tt&gt;. This was because same factory was being loaded by two different gems using different relative paths. Once I converted the code to &lt;a href="https://github.com/railsdog/spree/commit/389773b2f1039b3593546de6f66231941e9bdb5a"&gt;use absolute path&lt;/a&gt; this problem was fixed.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;The require command is not suppossed to load a file that is already loaded. However while requiring a file, ruby VM looks at the path and the file name and it can cause some strange behavior.&lt;/p&gt;

&lt;h2&gt;Basics&lt;/h2&gt;

&lt;p&gt;Ruby allows two ways to load file: require and load. &lt;tt&gt;load&lt;/tt&gt; will load a file every single time while require will not load already loaded file.&lt;/p&gt;

&lt;p&gt;I have two files here in the same directory: &lt;tt&gt;main.rb&lt;/tt&gt; and &lt;tt&gt;foo.rb&lt;/tt&gt; . Here is what files look like.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #main.rb
    require 'foo'
    require 'foo'
  &lt;/code&gt;
&lt;/pre&gt;




&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #foo.rb
    puts "I am being loaded"
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If you execute &lt;tt&gt;ruby main.rb&lt;/tt&gt; you will see a single message "I am being loaded". That is because once a file is already loaded then it wil not be loaded second time.&lt;/p&gt;

&lt;h2&gt;Require using relative path&lt;/h2&gt;

&lt;p&gt;Now let's change the &lt;tt&gt;main.rb&lt;/tt&gt; to require &lt;tt&gt;foo.rb&lt;/tt&gt; using relative path. After the change file looks like this&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #main.rb
    require 'foo'
    require '../dev/foo'
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If you execute &lt;tt&gt;ruby main.rb&lt;/tt&gt; you will see the puts message twice. That is because ruby is loading &lt;tt&gt;foo.rb&lt;/tt&gt; twice.&lt;/p&gt;

&lt;h2&gt;$LOADED_FEATURES - keeping track of what's loaded and what's not&lt;/h2&gt;

&lt;p&gt;ruby uses $LOADED_FEATURES , a global constant, to keep track of what's loaded and what's not loaded. Here is an updated version of &lt;tt&gt;main.rb&lt;/tt&gt;.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    #main.rb
    require 'foo'
    require 'foo'
    puts $LOADED_FEATURES.inspect
    require '../dev/foo'
    puts $LOADED_FEATURES.inspect
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If you execute &lt;tt&gt;ruby main.rb&lt;/tt&gt; you will see following message.&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    I am being loaded
    ["enumerator.so", "foo.rb"]
    I am being loaded
    ["enumerator.so", "foo.rb", "../dev/foo.rb"]
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;As you can see ruby while loading a file also stores the path that was provided. Now that we know that ruby looks at the path fix is easy. Here  is fixed version.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    require File.expand_path('../foo', __FILE__)
    puts $LOADED_FEATURES.inspect
    require File.expand_path('../../lab/foo', __FILE__)
    puts $LOADED_FEATURES.inspect
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;If you execute &lt;tt&gt;ruby main.rb&lt;/tt&gt; you will see following message.&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    I am being loaded
    ["enumerator.so", "/Users/nsingh/dev/scratch/lab/foo.rb"]
    ["enumerator.so", "/Users/nsingh/dev/scratch/lab/foo.rb"]
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;As you can see &lt;tt&gt;foo.rb&lt;/tt&gt; is being loaded only once and that is because &lt;tt&gt;$LOADED_FEATURES&lt;/tt&gt; has the absolute path to &lt;tt&gt;foo.rb&lt;/tt&gt;.&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;There is no need to have abosolute path in all the cases. However in a complex case where a number of different files are being required and some of the required files in turn require more files dynamically then it is best to have a policy of everyone requiring file using absolute path.&lt;/p&gt;

&lt;p&gt;Recently in the &lt;a href="http://spreecommerce.com"&gt;Spree&lt;/a&gt; project, the tests started to fail with the message that &lt;tt&gt;factories are already loaded&lt;/tt&gt;. This was because same factory was being loaded by two different gems using different relative paths. Once I converted the code to &lt;a href="https://github.com/railsdog/spree/commit/389773b2f1039b3593546de6f66231941e9bdb5a"&gt;use absolute path&lt;/a&gt; this problem was fixed.&lt;/p&gt;
</content>
  <feedburner:origLink>http://railsdog.com/blog/blog/2011/01/19/require-using-expanded-path/</feedburner:origLink></entry>
  <entry>
    <title>Sticker Mule Launches Rails3 Version of their Site</title>
    <link href="http://feedproxy.google.com/~r/railsdog/~3/Y9L-_v6QL04/" rel="alternate" />
    <id>http://railsdog.com/blog/blog/2010/10/31/sticker-mule-launches-rails3-version/</id>
    <published>2010-10-31T18:00:00-04:00</published>
    <updated>2010-10-31T18:00:00-04:00</updated>
    <author>
      <name>Sean Schofield</name>
    </author>
    <summary type="html">&lt;p&gt;Rails Dog is pleased to announce that the StickerMule.com site has been upgraded to Rails 3.0.  This is a significant milestone because this is our first client site to run the new Rails3-compatible version of Spree.  The site has been running for several days now with only minor issues.  We've already fixed a few minor bugs in the Spree project as a result.  We know that the Spree community is anxiously awaiting the official release of Spree 0.30.0 and this brings us one step closer.&lt;/p&gt;

&lt;p&gt;We have upgraded several existing Spree extensions to be compatible with the new version of Spree and we've resolved several important issues that will help reduce the pain of existing sites that are looking to upgrade.  We've also developed a series of database migrations that will help migrate existing Spree sites with minimal disruption.&lt;/p&gt;

&lt;p&gt;With the successful upgrade of this client we are now ready to finalize the official Spree release.  While we're working on that, feel free to checkout the new site, especially if you're looking for some high qualtiy &lt;a href="http://www.stickermule.com"&gt;custom stickers&lt;/a&gt;.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Rails Dog is pleased to announce that the StickerMule.com site has been upgraded to Rails 3.0.  This is a significant milestone because this is our first client site to run the new Rails3-compatible version of Spree.  The site has been running for several days now with only minor issues.  We've already fixed a few minor bugs in the Spree project as a result.  We know that the Spree community is anxiously awaiting the official release of Spree 0.30.0 and this brings us one step closer.&lt;/p&gt;

&lt;p&gt;We have upgraded several existing Spree extensions to be compatible with the new version of Spree and we've resolved several important issues that will help reduce the pain of existing sites that are looking to upgrade.  We've also developed a series of database migrations that will help migrate existing Spree sites with minimal disruption.&lt;/p&gt;

&lt;p&gt;With the successful upgrade of this client we are now ready to finalize the official Spree release.  While we're working on that, feel free to checkout the new site, especially if you're looking for some high qualtiy &lt;a href="http://www.stickermule.com"&gt;custom stickers&lt;/a&gt;.&lt;/p&gt;
</content>
  <feedburner:origLink>http://railsdog.com/blog/blog/2010/10/31/sticker-mule-launches-rails3-version/</feedburner:origLink></entry>
  <entry>
    <title>Redirect Non-WWW Requests the Rails3 Way</title>
    <link href="http://feedproxy.google.com/~r/railsdog/~3/Ed_t5T8PguA/" rel="alternate" />
    <id>http://railsdog.com/blog/blog/2010/10/29/redirect-non-www-requests-the-rails3-way/</id>
    <published>2010-10-29T18:00:00-04:00</published>
    <updated>2010-10-29T18:00:00-04:00</updated>
    <author>
      <name>Sean Schofield</name>
    </author>
    <summary type="html">&lt;p&gt;Have you ever had a situation where you would like to redirect non-WWW traffic to a canonical WWW version?  In others words you want visitors to http://example.com to be redirected to http://www.example.com.  Until recently you basically had one of two options.&lt;/p&gt;

&lt;h2&gt;Option 1 - Server Configuration&lt;/h2&gt;

&lt;p&gt;One traditional way to achieve this redirect is via server configuration.  For Apache this would involve something like the following:&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;For Nginx you would configure the same redirect using the following&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    server {
      listen 80;
      server_name example.com;
      rewrite ^/(.*) http://www.example.com/$1 permanent;
    }
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Using server configuration to achieve this has two significant drawbacks.  The first problem is that the configuration is not typically stored with your source code and thus you're prone to lose/forget about it if you ever need to move your code to a new server.  The second problem is that you may not have access to your server configuration.  This is a particular problem when hosting with &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; which does not allow users to configure their web servers (which are multi-tenant.)&lt;/p&gt;

&lt;h2&gt;Option 2 - Use Some Type of Rack Middleware&lt;/h2&gt;

&lt;p&gt;Another common approach is to use some type of Rack middleware to achieve the redirection.  Up until recently I've been using this approach.  Here's an example I found from a recent &lt;a href="http://stackoverflow.com/questions/4046960/how-to-redirect-without-www-using-rails-3-rack"&gt;Stack Overflow search&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    class CanonicalRedirect
      def initialize(app)
        @app = app
      end

      def call(env)
        request = Rack::Request.new(env)

        if request.host =~ /^example.com/
          [301, {"Location" =&gt; request.url.sub("//", "//www.")}, self]
        else
          @app.call(env)
        end
      end

      def each(&amp;block)
      end
    end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;You'll also need to tweak the configuration in your application.rb file.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    config.autoload_paths += %W(#{config.root}/lib)
    config.middleware.use "CanonicalRedirect"
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;This approach certainly works but it always left me wanting.  Why do we need an entire class plus extra configuration just to do some simple redirects?&lt;/p&gt;

&lt;h1&gt;A Better Way: Use Rails3 Routing Awesomeness&lt;/h1&gt;

&lt;p&gt;Rails 3.0 offers a lot of cool new improvements to routing.  The &lt;a href="http://guides.rubyonrails.org/routing.html"&gt;Rails documentation on routing&lt;/a&gt; is quite extensive but it doesn't cover all of the interesting cases.  In fact, a basic Google search turned up nothing on this approach.  After a little bit of experimentation I came up with a solution.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    Foo::Application.routes.draw do
      constraints(:host =&gt; /example.com/) do
        root :to =&gt; redirect("http://www.example.com")
        match '/*path', :to =&gt; redirect {|params| "http://www.example.com/#{params[:path]}"}
      end
    end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;This is much better.  Just five lines of code and completely portable no matter where you deploy your application!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Updated:&lt;/strong&gt; Fixed the Rails 3 example to deal with "Regexp anchor characters are not allowed in routing requirements" exception in Rails 3.0.3 and greater.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Have you ever had a situation where you would like to redirect non-WWW traffic to a canonical WWW version?  In others words you want visitors to http://example.com to be redirected to http://www.example.com.  Until recently you basically had one of two options.&lt;/p&gt;

&lt;h2&gt;Option 1 - Server Configuration&lt;/h2&gt;

&lt;p&gt;One traditional way to achieve this redirect is via server configuration.  For Apache this would involve something like the following:&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com
    RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;For Nginx you would configure the same redirect using the following&lt;/p&gt;

&lt;pre&gt;
  &lt;code&gt;
    server {
      listen 80;
      server_name example.com;
      rewrite ^/(.*) http://www.example.com/$1 permanent;
    }
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;Using server configuration to achieve this has two significant drawbacks.  The first problem is that the configuration is not typically stored with your source code and thus you're prone to lose/forget about it if you ever need to move your code to a new server.  The second problem is that you may not have access to your server configuration.  This is a particular problem when hosting with &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; which does not allow users to configure their web servers (which are multi-tenant.)&lt;/p&gt;

&lt;h2&gt;Option 2 - Use Some Type of Rack Middleware&lt;/h2&gt;

&lt;p&gt;Another common approach is to use some type of Rack middleware to achieve the redirection.  Up until recently I've been using this approach.  Here's an example I found from a recent &lt;a href="http://stackoverflow.com/questions/4046960/how-to-redirect-without-www-using-rails-3-rack"&gt;Stack Overflow search&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    class CanonicalRedirect
      def initialize(app)
        @app = app
      end

      def call(env)
        request = Rack::Request.new(env)

        if request.host =~ /^example.com/
          [301, {"Location" =&gt; request.url.sub("//", "//www.")}, self]
        else
          @app.call(env)
        end
      end

      def each(&amp;block)
      end
    end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;You'll also need to tweak the configuration in your application.rb file.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    config.autoload_paths += %W(#{config.root}/lib)
    config.middleware.use "CanonicalRedirect"
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;This approach certainly works but it always left me wanting.  Why do we need an entire class plus extra configuration just to do some simple redirects?&lt;/p&gt;

&lt;h1&gt;A Better Way: Use Rails3 Routing Awesomeness&lt;/h1&gt;

&lt;p&gt;Rails 3.0 offers a lot of cool new improvements to routing.  The &lt;a href="http://guides.rubyonrails.org/routing.html"&gt;Rails documentation on routing&lt;/a&gt; is quite extensive but it doesn't cover all of the interesting cases.  In fact, a basic Google search turned up nothing on this approach.  After a little bit of experimentation I came up with a solution.&lt;/p&gt;

&lt;pre&gt;
  &lt;code class="ruby"&gt;
    Foo::Application.routes.draw do
      constraints(:host =&gt; /example.com/) do
        root :to =&gt; redirect("http://www.example.com")
        match '/*path', :to =&gt; redirect {|params| "http://www.example.com/#{params[:path]}"}
      end
    end
  &lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;This is much better.  Just five lines of code and completely portable no matter where you deploy your application!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Updated:&lt;/strong&gt; Fixed the Rails 3 example to deal with "Regexp anchor characters are not allowed in routing requirements" exception in Rails 3.0.3 and greater.&lt;/p&gt;
</content>
  <feedburner:origLink>http://railsdog.com/blog/blog/2010/10/29/redirect-non-www-requests-the-rails3-way/</feedburner:origLink></entry>
  <entry>
    <title>Railsdog is Hiring</title>
    <link href="http://feedproxy.google.com/~r/railsdog/~3/YMCtbFtScs4/" rel="alternate" />
    <id>http://railsdog.com/blog/blog/2010/04/06/railsdog-is-hiring/</id>
    <published>2010-04-06T13:00:00-04:00</published>
    <updated>2010-04-06T13:00:00-04:00</updated>
    <author>
      <name>Sean Schofield</name>
    </author>
    <summary type="html">&lt;p&gt;We're now hiring experienced Ruby/Rails developers for full time
positions.  Developers will be working primarily on client work
related to the &lt;a href="http://spreecommerce.com"&gt;Spree e-commerce&lt;/a&gt; project.
This is a great opportunity to work for a company that is passionate
about open source.  MS Word is the only commercial software you are
likely to encounter in our office!  Come join our team and help shape
the future of e-commerce.&lt;/p&gt;

&lt;p&gt;Candidates must be local to the Washington, D.C. Metro Area.
Developers will work on site but can expect flexible scheduling and
the ability to work from home.  Our office is located at the
Friendship Heights Metro. Prior experience with Spree is not necessary
but we do require at least 12 month of experience with Ruby/Rails.  We
offer full benefits including health insurance.&lt;/p&gt;

&lt;p&gt;Please send an email to
&lt;a href="mailto://jobs@railsdog.com"&gt;jobs@railsdog.com&lt;/a&gt; with your resume
and/or LinkedIn profile.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;We're now hiring experienced Ruby/Rails developers for full time
positions.  Developers will be working primarily on client work
related to the &lt;a href="http://spreecommerce.com"&gt;Spree e-commerce&lt;/a&gt; project.
This is a great opportunity to work for a company that is passionate
about open source.  MS Word is the only commercial software you are
likely to encounter in our office!  Come join our team and help shape
the future of e-commerce.&lt;/p&gt;

&lt;p&gt;Candidates must be local to the Washington, D.C. Metro Area.
Developers will work on site but can expect flexible scheduling and
the ability to work from home.  Our office is located at the
Friendship Heights Metro. Prior experience with Spree is not necessary
but we do require at least 12 month of experience with Ruby/Rails.  We
offer full benefits including health insurance.&lt;/p&gt;

&lt;p&gt;Please send an email to
&lt;a href="mailto://jobs@railsdog.com"&gt;jobs@railsdog.com&lt;/a&gt; with your resume
and/or LinkedIn profile.&lt;/p&gt;
</content>
  <feedburner:origLink>http://railsdog.com/blog/blog/2010/04/06/railsdog-is-hiring/</feedburner:origLink></entry>
  <entry>
    <title>StickerMule.com Has Launched</title>
    <link href="http://feedproxy.google.com/~r/railsdog/~3/-2Kpc0zZsnY/" rel="alternate" />
    <id>http://railsdog.com/blog/blog/2010/04/01/announcing-sticker-mule/</id>
    <published>2010-04-01T16:00:00-04:00</published>
    <updated>2010-04-01T16:00:00-04:00</updated>
    <author>
      <name>Sean Schofield</name>
    </author>
    <summary type="html">&lt;p&gt;We are pleased to announce the launch of our latest client site: &lt;a href="http://www.stickermule.com"&gt;stickermule.com&lt;/a&gt;.  This is an e-commerce site where you can purchase &lt;a href="http://www.stickermule.com"&gt;custom stickers&lt;/a&gt; for just about every scenario you can think of.  Customers upload their artwork and specify the exact size and quantity combinations they desire.  The StickerMule artists will send you a proof which you need to sign off on before the stickers are printed.  You can go back and forth with the designers until you are satisfied with the design.  They also offer custom cut stickers so you can get stickers that are cut in the shape of your artwork.  We have a bunch of them in the office with the Rails Dog logo.&lt;/p&gt;

&lt;p&gt;&lt;img src="/img/blog/sticker-mule.png" alt="StickerMule.com" /&gt;&lt;/p&gt;

&lt;p&gt;There are several technical features of this site that we think are pretty interesting.  The custom artwork is uploaded via a Flash uploader and the images themselves are stored remotely using &lt;a href="http://s3.amazonaws.com"&gt;Amazon's S3 cloud storage&lt;/a&gt;.  The site itself is running on &lt;a href="http://spreecommerce.com"&gt;Spree&lt;/a&gt; with several custom screens on the backend to handle their specific business rules pertaining to artwork.  We also deployed the site "in the cloud" using &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt;.  Heroku is an excellent platform that we have been following for quite some time.  We're quite pleased with Heroku so far and we'll be doing a more detailed write up on using Spree with Heroku in the near future.&lt;/p&gt;

&lt;p&gt;If you're interested in getting some custom stickers made for your business or website be sure to pay these guys a visit.  Use the promo code "SPREE" between now and April 31 and get $50 off your order.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;We are pleased to announce the launch of our latest client site: &lt;a href="http://www.stickermule.com"&gt;stickermule.com&lt;/a&gt;.  This is an e-commerce site where you can purchase &lt;a href="http://www.stickermule.com"&gt;custom stickers&lt;/a&gt; for just about every scenario you can think of.  Customers upload their artwork and specify the exact size and quantity combinations they desire.  The StickerMule artists will send you a proof which you need to sign off on before the stickers are printed.  You can go back and forth with the designers until you are satisfied with the design.  They also offer custom cut stickers so you can get stickers that are cut in the shape of your artwork.  We have a bunch of them in the office with the Rails Dog logo.&lt;/p&gt;

&lt;p&gt;&lt;img src="/img/blog/sticker-mule.png" alt="StickerMule.com" /&gt;&lt;/p&gt;

&lt;p&gt;There are several technical features of this site that we think are pretty interesting.  The custom artwork is uploaded via a Flash uploader and the images themselves are stored remotely using &lt;a href="http://s3.amazonaws.com"&gt;Amazon's S3 cloud storage&lt;/a&gt;.  The site itself is running on &lt;a href="http://spreecommerce.com"&gt;Spree&lt;/a&gt; with several custom screens on the backend to handle their specific business rules pertaining to artwork.  We also deployed the site "in the cloud" using &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt;.  Heroku is an excellent platform that we have been following for quite some time.  We're quite pleased with Heroku so far and we'll be doing a more detailed write up on using Spree with Heroku in the near future.&lt;/p&gt;

&lt;p&gt;If you're interested in getting some custom stickers made for your business or website be sure to pay these guys a visit.  Use the promo code "SPREE" between now and April 31 and get $50 off your order.&lt;/p&gt;
</content>
  <feedburner:origLink>http://railsdog.com/blog/blog/2010/04/01/announcing-sticker-mule/</feedburner:origLink></entry>
</feed>
