如何从ActiveResource获取响应标头?

时间:2014-01-07 01:07:30

标签: activeresource

我正在使用ActiveResource 4.0,我需要让分页工作。我在服务器端设置了响应头,但我无法在客户端读取它们。

我正在使用这篇精彩的博文: http://javiersaldana.com/2013/04/29/pagination-with-activeresource.html

我正在尝试从响应中读取标题: ActiveResource::Base.connection.response

但是我收到了这个错误: undefined method 'response' for #<ActiveResource::Connection:0x007f9a4f9692b8>

如何获取响应标头?

1 个答案:

答案 0 :(得分:0)

宝石“activeresource-response”来救援。 https://github.com/Fivell/activeresource-response

示例,假设服务器返回标题以进行分页 X-限制,X轴偏移量,X-总

class Order < ActiveResource::Base
  self.format = :json
  self.site = 'http://0.0.0.0:3000/'
  self.element_name = "order" 
  add_response_method :http_response  # our new method for returned objects 
end



class OrdersController < ApplicationController
  def index
    orders = Order.all(:params=>params)     
    @orders = Kaminari::PaginatableArray.new(
      orders,{
              :limit => orders.http_response['X-limit'].to_i,
            :offset =>orders.http_response['X-offset'].to_i,
            :total_count => orders.http_response['X-total'].to_i
      }) 
  end
end