Easier Ajax accessbility with Rails 1.1
March 10th, 2006David 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.
Tweet