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:


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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s