Get kaminari pagination links in the JSON generated by the active model serializer

泪湿孤枕 提交于 2019-12-24 16:33:54

问题


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:


回答1:


In the latest commit available you need to use:

resource = ActiveModel::SerializableResource.new(@admins)
resource.to_json(serialization_context: ActiveModelSerializers::SerializationContext.new(request))


来源:https://stackoverflow.com/questions/36301450/get-kaminari-pagination-links-in-the-json-generated-by-the-active-model-serializ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!