2016-11-17 76 views
0

決定使用Redis JSONify我的網站。我如何獲得will_paginate與json一起使用?will_paginate JSON數組

# games.html.erb 
<%= js_will_paginate @games, renderer: BootstrapPagination::Rails, class: 'pagination-sm', previous_label: "&#8592;".html_safe, next_label: "&#8594;".html_safe, page_links: false %> 

這是我的錯誤:

# undefined method `total_pages' for #<Array:0x007f90ebc05cf0> ln 23 of will_paginate_helper.rb 
ln23: will_paginate(collection, options.merge(:renderer => WillPaginateHelper::WillPaginateJSLinkRenderer)) 

而且我will_paginate_helper:

# will_paginate_helper.rb 
module WillPaginateHelper 
    class WillPaginateJSLinkRenderer < BootstrapPagination::Rails 
    def prepare(collection, options, template) 
     options[:params] ||= {} 
     options[:params]['_'] = nil 
     super(collection, options, template) 
    end 

    protected 
    def link(text, target, attributes = {}) 
     if target.is_a? Fixnum 
     attributes[:rel] = rel_value(target) 
     target = url(target) 
     end 

     @template.link_to(target, attributes.merge(remote: true)) do 
     text.to_s.html_safe 
     end 
    end 
    end 

    def js_will_paginate(collection, options = {}) 
    will_paginate(collection, options.merge(:renderer => WillPaginateHelper::WillPaginateJSLinkRenderer)) 
    end 
end 

,當我與nomethoderror例外,下面的CLI玩耍......

>> collection 
=> [{"id"=>8199, "start_time"=>nil, "game_date"=>"2016-10-23", ... etc }] 
>> collection.first 
=> {"id"=>8199, "start_time"=>nil, ... etc 

我需要轉換collection某些will_paginate可以使用或者我重寫/覆蓋js_will_paginate?謝謝!

回答

0

想通了。

添加require 'will_paginate/array'will_paginate_helper.rb然後分頁的採集後.to_json,例如在我的幫助(或控制器):

像以前一樣
games = Game.order(game_date: :desc).postgame.to_json 
@games = Oj.load games 
@games = @games.paginate(page: params[:page], per_page: 10) 

工程與will_paginate或js_will_paginate。