will_paginate JSON 数组
will_paginate JSON array
决定使用 Redis 对我的站点进行 JSONify。如何让 will_paginate
与 json 一起工作?
# games.html.erb
<%= js_will_paginate @games, renderer: BootstrapPagination::Rails, class: 'pagination-sm', previous_label: "←".html_safe, next_label: "→".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 可以使用的东西,或者我 re-write/override js_will_paginate
?谢谢!
想通了。
将 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。
决定使用 Redis 对我的站点进行 JSONify。如何让 will_paginate
与 json 一起工作?
# games.html.erb
<%= js_will_paginate @games, renderer: BootstrapPagination::Rails, class: 'pagination-sm', previous_label: "←".html_safe, next_label: "→".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 可以使用的东西,或者我 re-write/override js_will_paginate
?谢谢!
想通了。
将 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。