Get kaminari pagination links in the JSON generate

2019-08-12 22:01发布

I am trying to convert @admins to JSON using the AdminSerializer

#app/serializers/admin_serializer.rb
class AdminSerializer < ActiveModel::Serializer
  attributes :id, :email, :access_locked?
end

where Admins is>> @admins = @search.result(:distinct => true).page(params[:page][:number]).per(10) and @search = Admin.search(params[:q])

When I execute this command>> ActiveModel::SerializableResource.new(@admins.to_a).as_json I do get the desired JSON, but the pagination links are missing from the JSON received, as they were lost while converting the @admins to array using to_a. However, when I execute render :json => @admins , I get the complete JSON with the pagination links in it, as shown inn the screenshot below: enter image description here

1条回答
太酷不给撩
2楼-- · 2019-08-12 22:30

In the latest commit available you need to use:

resource = ActiveModel::SerializableResource.new(@admins)
resource.to_json(serialization_context: ActiveModelSerializers::SerializationContext.new(request))
查看更多
登录 后发表回答