Wed 6 Dec 2006
While it’s nothing major, Rails recently got the ability to render JSON a little more easily:
- 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:
- class PeopleController < ActionController::Base
- …
- def show
- @person = Person.find(params[:id])
- respond_to do |format|
- format.html
- format.xml { render
ml => @person.to_xml } - # this can be called with /people/1.json?callback=loadPerson
- # and will generate ‘loadPerson({name: "Brian", skills: "bowhunting, nunchuck, computer hacking"})’
- format.json { render :json => @person.to_json, :callback => params[:callback] }
- end
- end
- …
- 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!