开发者

problem with will_paginate with render json in rails 2.3.11

I am trying to upgrade my rails gem from 2.3.2 to 2.3.11. However, I got some problems with will_paginate 2.3.15 and render json back.

module WillPaginateHelpers
    WillPaginate::Collection.class_eval do
      alias :to_json_without_paginate :to_json

        def to_json(options = {})
          hash = { :current_page => current_page,
            :per_page => per_page,
            :total_entries => total_entries,
            :total_pages => total_pages,
            :items => to_a
          }

          hash.to_json(options)
        end
    end
end

Previously, 开发者_JS百科the code above could work with:

@products = Product.paginate(:page => 1, :per_page => 20)
render :json => @products

However, with rails 2.3.11, it comes up with error "object references itself" unless i need to code this way: render :json => @products.to_json. How to fix this? What happened with render :json => @products?


I've added this to an initializer:

class WillPaginate::Collection
  def as_json options={}
    {
      :total_entries => self.total_entries,
      :current_page => self.current_page,
      :total_pages => self.total_pages,
      :per_page => self.per_page,
      :items => super
    }
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜