问题
I'm using the Twilio API in a rails app to show a user a list of their recordings. Say a user has 11 recordings total, and I'm showing them 3 per page.
twilio_controller.rb
def calls
@user = current_user
@account_sid = @user.twilio_account_sid
@auth_token = @user.twilio_auth_token
@page_size = 3
@page = params[:page_id] || 0
@sub_account_client = Twilio::REST::Client.new(@account_sid, @auth_token)
@subaccount = @sub_account_client.account
@recordings = @subaccount.recordings
@recordingslist = @recordings.list({:page_size => @page_size, :page => @page})
end
calls.html.erb
<% @recordingslist.each do |recording| %>
<tr>
<td><%= recording.sid %></td>
</tr>
<% end %>
<%= link_to "Next Page", twilio_calls_path(@page + 1) %>
routes.rb
#twilio routes
post 'twilio/callhandler'
get 'twilio/calls'
match 'twilio/calls' => 'twilio#page', :as => :twilio_page # Allow `recordings/page` to return the first page of results
match 'twilio/calls/:page_id' => 'twilio#page', :as => :twilio_page
Paging info is built into the Twilio response such that
@recordingslist.next_page
gives me the next 3 recordings (verified in rails console). How do I link to that so that when a user clicks the link, the table loads the next 3 results?
Thanks!
回答1:
I would recommend utilizing the paging functionality that ships with twilio-ruby. According to the docs:
ListResource.list() accepts paging arguments.
Start by create a route for your Twilio list view. Make sure you can pass a page_id parameter – this is how your controller action will know which page to render:
# config/routes.rb
match 'recordings/page/:page_id' => 'twilio#page', :as => :twilio_page
match 'recordings/page' => 'twilio#page', :as => :twilio_page # Allow `recordings/page` to return the first page of results
Then, in the page action, pull the page_id parameter (or set if to 1 if there is no page_id, and pass the page_number and page_size as arguments to @recordings.list:
# app/controllers/twilio_controller.rb
def page
page_size = 3
@page = params[:page_id] || 1
@sub_account_client = Twilio::REST::Client.new(@account_sid, @auth_token)
@subaccount = @sub_account_client.account
@recordings = @subaccount.recordings
@recordingslist = @recordings.list({:page_size => page_size, :page => page)})
end
Finally, in your view, pass the page number to twilio_page_path in your link_helper – just make sure to adjust the page number accordingly (+1 for the next page, -1 for the previous page:
# view
<%= link_to "Next Page", twilio_page_path(@page.to_i + 1) %>
<%= link_to "Previous Page", twilio_page_path(@page.to_i - 1) %>
Note that – if you're at the start or end of your list – you may inadvertently end up passing an invalid page_id. Therefore, you may want to implement some exception handling in your page controller action:
# app/controllers/twilio_controller.rb
def page
begin
@page = params[:page_id] || 1 # If `page_id` is valid
rescue Exception => e
@page = 1 # If `page_id` is invalid
end
# Remaining logic...
end
回答2:
You can use a gem like Kaminari for Pagination.
https://github.com/amatsuda/kaminari
来源:https://stackoverflow.com/questions/20958777/create-a-link-for-next-page-in-rails