2016-11-17 9 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 

01に変換する必要がありますかどのようなwill_paginateにが対応できますか、またはjs_will_paginateを書き直し/上書きできますか?ありがとうございました!

答えて

0

それを実演しました。

require 'will_paginate/array'から私のwill_paginate_helper.rbに加えた後、コレクションAFTER .to_json、例えば、私のヘルパー(またはあなたのコントローラ)で:ただ以前のようにwill_paginateまたはjs_will_paginateと

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

作品。