在 rails 中创建下一页的链接 [英] Create a link for next page in rails
问题描述
我在 rails 应用程序中使用 Twilio API 向用户显示他们的录音列表.假设一个用户总共有 11 个录音,我每页显示 3 个.
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
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
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
分页信息内置于 Twilio 响应中,使得
Paging info is built into the Twilio response such that
@recordingslist.next_page
给我接下来的 3 个录音(在 rails 控制台中验证).如何链接到该链接,以便当用户单击链接时,表格会加载接下来的 3 个结果?
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?
谢谢!
推荐答案
我建议使用 twilio-ruby 附带的分页功能.根据文档:
I would recommend utilizing the paging functionality that ships with twilio-ruby. According to the docs:
ListResource.list() 接受分页参数.
ListResource.list() accepts paging arguments.
首先为您的 Twilio 列表视图创建一个路由.确保您可以传递 page_id
参数 - 这就是您的控制器操作将知道要呈现哪个页面的方式:
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
然后,在page
动作中,拉取page_id
参数(如果没有page_id<,则设置if为
1
/code>,并将 page_number
和 page_size
作为参数传递给 @recordings.list
:
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
最后,在您的视图中,将页码传递给 link_helper
中的 twilio_page_path
– 只需确保相应地调整页码 (+1
表示下一页,-1
表示上一页:
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) %>
请注意,如果您位于列表的开头或结尾,您可能会无意中传递无效的 page_id
.因此,您可能希望在您的 page
控制器操作中实现一些异常处理:
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
这篇关于在 rails 中创建下一页的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!