ruby-on-rails – 使用没有活动记录的will_paginate
发布时间:2020-12-17 03:47:36 所属栏目:百科 来源:网络整理
导读:主持人: 应用程序/主持人/ games_presenter.rb class GamesPresenter attr_reader :games,:next_page,:previous_page def initialize json @games = json['machine-games'] paging = json['paging'] if paging paging['next'] next_page_query = paging['ne
主持人:
应用程序/主持人/ games_presenter.rb class GamesPresenter attr_reader :games,:next_page,:previous_page def initialize json @games = json['machine-games'] paging = json['paging'] if paging && paging['next'] next_page_query = paging['next'].match(/?.*/)[0] @next_page = "/machine_games/search#{next_page_query}" end if paging && paging['previous'] previous_page_query = paging['previous'].match(/?.*/)[0] @previous_page = "/machine_games/search#{previous_page_query}" end end end 控制器动作: def show # ... @presenter = GamesPresenter.new(json) end 观点: <% @presenter.games.each do |game| %> ... <% end %> <%= link_to "Previous",@presenter.previous_page %> <%= link_to "Next",@presenter.next_page %> 并且为了告诉Rails加载apps / presenters /目录以及models /,controllers /,views /等,将其添加到config / application.rb: config.after_initialize do |app| app.config.paths.add 'app/presenters',:eager_load => true end 我想知道如何在上述情况下使用will_paginate? .谢谢. 解决方法
假设@presenter.games是一个数组,试试这个:
# Gemfile gem 'will_paginate' # /config/initializers/will_paginate_array.rb require 'will_paginate/collection' Array.class_eval do def paginate(page = 1,per_page = 15) page = 1 if page.blank? # To fix weird params[:page] = nil problem WillPaginate::Collection.create(page,per_page,size) do |pager| pager.replace self[pager.offset,pager.per_page].to_a end end end # /app/controllers/games_controller.rb def show @presenter = GamesPresenter.new(json) @games = @presenter.games.paginate(params[:page],5) end # /app/views/games/index.html.erb <% @games.each do |game| %> ... <% end %> <%= will_paginate @games %> 这基本上将.paginate方法添加到所有数组.有关此问题的更多文档可在https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/collection.rb找到 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |