プレゼンター:
app/presenters/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 %>
そして、モデル/、コントローラー/、ビュー/などとともにapps/presenters/ディレクトリをロードするようにRailsに指示するには、これをconfig/application.rbに追加します。
config.after_initialize do |app|
app.config.paths.add 'app/presenters', :eager_load => true
end
上記のケースで will_paginate を使用する方法を知りたいですか? 。ありがとうございました。