Thomas Fuchs
Hi, I'm Thomas Fuchs. I'm the author of Zepto.js, of script.aculo.us, and I'm a Ruby on Rails core alumnus. With Amy Hoy I'm building cheerful software, like Noko Time Tracking and Every Time Zone and write books like Retinafy.me.
   Want me to speak at your conference? Contact me!

Easier Ajax accessbility with Rails 1.1

March 10th, 2006

David writes about the new HTTP accept header handling capability coming in Rails 1.1:


class CommentController < ActionController::Base
  def create
    @comment = Comment.create(params[:comment])

    respond_to do |type|
      type.html { redirect_to :action => "index" }
      type.js
      type.xml  do
        headers["Location"] =
          url_for(:action => "show", :id => @comment.id)
        render(:nothing, :status => "201 Created")
      end
    end
  end
end


This is a sample controller for adding comments to a weblog. It’s able to serve old browsers, Ajaxified browsers, and API access for the blog. Three clients, same controller logic.

Next to allow for totally easy creation of API functionalities in your application, this is also great news for all accessiblilty buffs—easily reusable controller actions for ajax and non-ajax calls are an beta gem away.