render :json does not accept options render :json does not accept options ruby-on-rails ruby-on-rails

render :json does not accept options


I've done something similar with render :json. This is what worked for me:

respond_to do |format|    format.html # index.html.erb    format.json  { render :json => @things.to_json(:include => { :photos => { :only => [:id, :url] } }) }end


I guess this article can be useful for you - Rails to_json or as_json? by Jonathan Julian.

The main thought is that you should avoid using to_json in controllers. It is much more flexible to define as_json method in your model.

For instance:

In your Thing model

def as_json(options={})  super(:include => :photos)end

And then you can write in your controller just

render :json => @things


Managing complex hashes in your controllers gets ugly fast.

With Rails 3, you can use ActiveModel::Serializer. See http://api.rubyonrails.org/classes/ActiveModel/Serialization.html

If you're doing anything non-trivial, see https://github.com/rails-api/active_model_serializers. I recommend creating separate serializer classes to avoid cluttering your models and make tests easier.

class ThingSerializer < ActiveModel::Serializer  has_many :photos  attributes :name, :whateverend# ThingsControllerdef index  render :json => @thingsend# test it outthing = Thing.new :name => "bob"ThingSerializer.new(thing, nil).to_json