wmakley / tiny_serializer

Simple Ruby JSON Serialization DSL. Replaces active_model_serializers.
MIT License
53 stars 2 forks source link

ArgumentError (wrong number of arguments (given 1, expected 0)) #3

Closed GuskiS closed 6 years ago

GuskiS commented 6 years ago

When I do:

render json: { data: UserSerializer.new(current_user) }

I get ArgumentError (wrong number of arguments (given 1, expected 0)) there isn't much of stacktrace, but it points to https://github.com/wmakley/simple_serializer/blob/master/lib/simple_serializer.rb#L99

However, if I do:

render json: { data: UserSerializer.new(current_user).as_json }

it works just fine.

I tried adding:

  def as_json(data)
    binding.pry
  end

to serializer, data seems to be {} 🤔

wmakley commented 6 years ago

This is my recommended usage now:

render json: { data: UserSerializer.new(current_user).serialize }

I just added some parameters to fix your use case though. I forgot that ActiveSupport expected #as_json and #to_json to take an options argument.

I'm not keen on the idea that a serializer is in some way a stand in for the object (it is one-way converter) but they can be used that way now if it makes things easier.

GuskiS commented 6 years ago

Btw, just tried adding:

  def as_json(data = {})
    super()
  end

and it seems to be working.

wmakley commented 6 years ago

Yeah the issue was my as_json implementation didn't take any arguments.