While it’s nothing major, Rails recently got the ability to render JSON a little more easily:

  1. render :json => items.to_json

This sets the content type to “application/json” and renders whatever text passed. But wait! There’s more! You can also start implementing Yahoo!-esque REST web services with JS callbacks like so:

  1. class PeopleController < ActionController::Base
  2.  
  3. def show
  4. @person = Person.find(params[:id])
  5. respond_to do |format|
  6. format.html
  7. format.xml  { render :x ml => @person.to_xml }
  8. # this can be called with /people/1.json?callback=loadPerson
  9. # and will generate ‘loadPerson({name: "Brian", skills: "bowhunting, nunchuck, computer hacking"})’
  10. format.json { render :json => @person.to_json, :callback => params[:callback] }
  11. end
  12. end
  13.  
  14. end

You might wonder why I’d bother blogging about this. It’s because this marks my 5th patch into Rails (well, maybe 4.5 since it was originally posted by someone else and I updated it with tests). w00t!