0

I am playing with Jose Valim's active_model_serializers Gem.

I am attempting to call a custom serializer in my controller, and it looks like :json is not using the ActiveModel::ArraySerializer as it should.

When I define the default active_model_serializer in the controller, the serializer works fine; however, when I call a custom one like this in the controller:

render :json => @project, :serializer => ProjectSerializer

I receive the following error:

wrong number of arguments (0 for 1)

I am using Ruby 1.9.2, and Rails 3.2.7. Any help is appreciated as I need to create a few different serializers for any given model.

Thanks!

hunterboerner
  • 1,264
  • 1
  • 11
  • 25
  • Are you sure the error is on that line? The signature of `render` is `render(*args, &block)`, so even if `@project` is `nil`, you wouldn't be getting this error. Also, what does your ProjectSerializer code look like? – jordanpg Aug 03 '12 at 20:32
  • 1
    If `@project` is an array, it should probably be named `@projects`. That won't fix the problem, but it would make more sense & help with debugging. – tee Aug 14 '12 at 19:58

1 Answers1

0

So I must have been on crack or something. the :serializer call only accepts array's of objects.

render :json => @project, :serializer => ProjectWithTaskEstimateSerializer

When you want to call the a serializer on each object within an array of objects you need to use each_serializer. I'm not sure how I missed this.

render :json => @project, :each_serializer => ProjectSerializer

  • When you call render with a json array, you can use both `:serializer` and `:each_serializer` options. `:serializer` would override ArraySerializer. `:each_serializer` would override the serializer for each object in the array. – tee Aug 14 '12 at 20:00