The Artima Developer Community
Sponsored Link

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

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rwdaigle

Posts: 312
Nickname: rwdaigle
Registered: Feb, 2003

Ryan is a passionate ruby developer with a strong Java background.
What's New in Edge Rails: Even Better Conditional GET Support Posted: Oct 24, 2008 9:22 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by rwdaigle.
Original Post: What's New in Edge Rails: Even Better Conditional GET Support
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps

Advertisement

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

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

Topic: What's New in Edge Rails: You Smell That? Previous Topic   Next Topic Topic: Rails Rumble 2008: Developing Run.Track.Run.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use