传递current_user并将链接链接到fast_jsonapi中

时间:2019-12-03 07:34:09

标签: ruby-on-rails ruby fastjsonapi

滑轨版本:6.0.0 Ruby版本:2.6.3

控制器

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

序列化器

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

错误:

ArgumentError (wrong number of arguments (given 3, expected 1..2)):

2 个答案:

答案 0 :(得分:1)

我尝试了另一种方法,它正在起作用,

在控制器中

areas = Area.all

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

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

在序列化器中

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

并且link键会在Serializer中自动呈现。


前端收到的示例输出:

{
  data: [
    {},
    {} // areas hash
  ],
  links: {
    first: "",
    last: "",
  }
}

答案 1 :(得分:0)

使用类似这样的内容:

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

序列化器

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