Adding json responses to all of your controller actions
published Jan 14, 2008
Gerald has decided to use JSON responses for the mobile phone app
we’ll be building at the intermediate Rails
workshop.
I’m a lazy guy by nature, so there was no way I was going to paste in
in to very controller method.
Now, the easiest way to do this would be to add JSON response to the scaffolding. The templates are in RAILS_ROOT/vendor/rails/lib/rails_generator/generators/components/scaffold/
. Open up the controller.rb
template and add in the JSON response shown above to the index, show, update and destroy methods and you’re done.
Unfortunately, I had already mostly developed the API when I realized that I had to add in the JSON responses (It’s not Gerald’s fault, he told me long ago). So, I decided to write a quick rake task to do the job for me. To use it, create a file called add_json.rake
in RAILS_ROOT/lib/tasks
and copy the following code in to it:
Call the task from RAILS_ROOT
like this:
rake rails:add_json
The task opens up every controller, and every time it finds a line that starts with format.xml
, it makes a copy of that line just below with every instance of ‘xml’ replaced with ‘json’. It’s pretty very brain-dead: it doesn’t check if the json response is already there, so don’t run it twice on the same project.