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.

Advertisements

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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

w

Connecting to %s