3

Rails version : 6.0.0 Ruby version : 2.6.3

controller

render json: AreaSerializer.new(
      Area.all, paginate(pagy), params: { current_user: current_user }
    ), status: ok

serializer

attributes :nodes do |area, params|
...
end

Error:

ArgumentError (wrong number of arguments (given 3, expected 1..2)):
Community
  • 1
  • 1
Srimathi G
  • 71
  • 5

2 Answers2

3

I tried another way and it was working,

In Controller

areas = Area.all

options = { 
            links: paginate(pagy), 
            params: { current_user: @current_user } 
          }

render json: AreaSerializer.new(areas, options),
       status: ok

In serializer

attributes :nodes do |area, params|
  area.users.where.not(id: params[:current_user].id)
end

And the links key is rendering automatically in Serializer.


Example output received by front-end:

{
  data: [
    {},
    {} // areas hash
  ],
  links: {
    first: "",
    last: "",
  }
}
Srimathi G
  • 71
  • 5
1

Use something like this:

render json: AreaSerializer.new(
               Area.all, 
               { params: { paginate: paginate(pagy), current_user: @current_user } }
    ), status: ok

Serializer

attributes :nodes do |area, params|
  ...
  params[:current_user]
  params[:paginate]
  ...
end