Rails respond_with customize conversion

Rails 3 introduces some cute stuff so that you can say respond_with(thing), and it will convert that to a json or xml representation if required. But how do you alter that, if (for instance) you want the to_json call to only use certain fields on the model?

You change this:

    respond_with(thing)

To this

    respond_with(thing, :only => [:name])

Arguments that you pass after the object in the respond_with call get passed through to the to_json method, you see. And arguments you lose stay lost forever.

Leave a comment