What's New in Edge Rails: Even Better Conditional GET Support

Posted by ryan
at 9:13 PM on Friday, October 24, 2008

This feature was released in Rails v2.2.1

We talked about the new conditional GET support in rails a couple months ago. As some of the comments alluded, the feature was somewhat cumbersome to use – especially by Ruby standards. Well, the feature has since been refined. So, read the original post to get the gist and come back here for the sugar.

Instead of manually setting properties directly on the response and querying the request to see if it’s fresh we have some higher-level accessors we can use. Observe (extending our previous example):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ArticlesController < ApplicationController

  def show
    @article = Article.find(params[:id])

    # If the request is stale according to the given timestamp and etag value
    # (i.e. it needs to be processed again) then execute this block
    if stale?(:last_modified => @article.published_at.utc, :etag => @article)
      respond_to do |wants|
        # ... normal response processing
      end
    end

    # If the request is fresh (i.e. it's not modified) then you don't need to do
    # anything. The default render checks for this using the parameters
    # used in the previous call to stale? and will automatically send a
    # :not_modified.  So that's it, you're done.
end

If you don’t have any special response processing and are using the default rendering mechanism (i.e. you’re not using respond_to or calling render yourself) then you’ve got an easy helper in fresh_when:

1
2
3
4
5
6
7
8
9
class ArticlesController < ApplicationController

  # This will automatically send back a :not_modified if the request is fresh, and
  # will render the default template (article.*) if it's stale.
  def show
    @article = Article.find(params[:id])
    fresh_when :last_modified => @article.published_at.utc, :etag => @article
  end
end

There you have it, the new and improved conditional GET support in Rails 2.2.

tags: ruby, rubyonrails

What's New in Edge Rails: Rails 2.2 Released - Summary of Features

Posted by ryan
at 8:13 PM on Friday, October 24, 2008

I’ve updated this post to reflect the features added after RC1, so consider this to be a full list of major 2.2 features

Rails 2.2 (RC1) was just released, let’s take a peek at some of the major new features (as determined by yours truly – feel free to pipe up with features that I’ve missed).

Rails 2.2.2 requires rubygems v 1.3.1, so before you upgrade make sure to do a sudo gem update --system.

Rails 2.2 Features

Note: The nested model mass assignment feature previously discussed has been delayed until after this 2.2 release. Just needs a little more time in the oven, apparently.

Enjoy, folks.

tags: ruby, rubyonrails

What's New in Edge Rails: You Smell That?

Posted by ryan
at 4:43 PM on Thursday, October 23, 2008

... smells like Rails v2.2 is right around the corner…

tags: ruby, rubyonrails