Ruby on rails 活动资源分页

Ruby on rails 活动资源分页,ruby-on-rails,activeresource,Ruby On Rails,Activeresource,通过具有活动资源的API进行分页的最佳方法是什么?我正在构建API和使用API的应用程序,所以我需要等式的两端 我见过人们在ActiveResource中为他们想要的页面设置标题(例如X-PERPAGE) 任何建议都很好。正在寻找最佳解决方案。1)使用下一个代码修补activeresource module ActiveResource class Connection alias_method :origin_handle_response, :handle_response

通过具有活动资源的API进行分页的最佳方法是什么?我正在构建API和使用API的应用程序,所以我需要等式的两端

我见过人们在ActiveResource中为他们想要的页面设置标题(例如X-PERPAGE)

任何建议都很好。正在寻找最佳解决方案。

1)使用下一个代码修补activeresource

module ActiveResource
  class Connection
    alias_method :origin_handle_response, :handle_response 
    def handle_response(response)
      Thread.current["active_resource_response_#{self.object_id}"]  = response
      origin_handle_response(response)
    end  

    def response
      Thread.current["active_resource_response_#{self.object_id}"] 
    end   
  end
end 
它将增加在执行rest方法后读取响应的可能性 2) 在服务器端,您可以使用kaminari执行下一步操作

@users = User.page(params[:page]).per(params[:per_page])
response.headers["total"] = @users.total_count.to_s
response.headers["offset"] = @users.offset_value.to_s
response.headers["limit"] = @users.limit_value.to_s
respond_with(@users)
3) 在客户端再次与卡米纳里

users = Users.all(:params=>params)
response = Users.connection.response
@users = Kaminari::PaginatableArray.new(
    users,
    {
      :limit => response['limit'].to_i ,
      :offset =>response['offset'].to_i ,
      :total_count => response['total'].to_i
    }   
)

ActiveResource 4.0.0.beta1引入了
ActiveResource::Collection
,它(根据源代码中的文档)是处理解析索引响应的包装器。可以设置
Post
类来处理它:

class Post < ActiveResource::Base
  self.site = "http://example.com"
  self.collection_parser = PaginatedCollection
end
class Post
您可以将分页数据嵌入API响应中,并使用
ActiveResource::Collection
检索它们


请参阅有关如何使用此功能的详细说明:

谢谢!!我会调查的!我为客户端添加了gem