ruby-on-railsrubyfastjsonapi

Pass current_user and links together in fast_jsonapi, in rails


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)):

Solution

  • 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: "",
      }
    }