Search code examples
ruby-on-railsrubyjsonrespond-with

Rails 3.2.16 JSON API behavior for respond_with while updating object


A bit of a conundrum around using JSON for updating resources:

  • If I use "POST" against /resource/:id - I get an undefined route
  • If I use "PUT" against a /resource/:id - I do get to the right method, but respond_with seems to return an empty content (see code below).

https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_controller/metal/responder.rb#L201

What's a GUI to do when it wants a nice reply for updating an object? Do I have to resort to explicit rendering?


Solution

  • The 204 No Content response is the default behavior from respond_with. You can either do the explicit renders if you want something different, or if you want a different behavior consistently you can define your own ActionController::Responder subclass.

    I'd recommend reading the source of the default responder, and checking out some other ones here:

    https://github.com/plataformatec/responders