主持人:
应用程序/演示者/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 ?。谢谢。