从API导航页面结果

时间:2014-01-05 21:52:57

标签: ruby-on-rails ruby pagination twilio

我在rails应用程序中有一个视图,我通过Twilio API向用户显示他们最新的50个录音。

虽然返回的page_size上限为50,但API会将URI返回到返回列表的下一页,上一页,第一页和最后一页。 (https://www.twilio.com/docs/api/rest/response#response-formats-list-paging-information

我无法理解如何在该表的底部链接到这些结果,以便当用户点击“下一页”时,页面会刷新并且表格会填充接下来的50个记录。

以下是表格:

<% @recordings.list({}).each do |recording| %>

  <tr>
     <td><%= recording.sid %></td>
     <td><%= recording.duration %>sec</td>
     <td><%= recording.from %></td>
     <td><%= recording.to %></td>
 </tr>

<% end %>

<%= link_to "next page", "?" %>

当我在rails控制台中查看时,我可以在响应中看到“next_page_uri”,我只是不确定我如何“链接”它以填充表格。

编辑** 我通过他们的API资源管理器发出请求以显示输出(以JSON格式) - 我将页面结果缩短为一个请求,以便更容易阅读。

{
      "first_page_uri": "/2010-04-01/Accounts/ACb76fd7689d66a37b3defd366bc3caf12/Recordings.json?AccountSid=ACb76fd7689d66a37b3defd366bc3caf12&Page=0&CSRF=1388959981-bedd066b4f6119c70465377bb8bc012a72ea7b13be683a0819b065aa9db559c7&PageSize=1&Format=json",
      "num_pages": 178,
      "previous_page_uri": null,
      "uri": "/2010-04-01/Accounts/ACb76fd7689d66a37b3defd366bc3caf12/Recordings.json?Format=json&AccountSid=ACb76fd7689d66a37b3defd366bc3caf12&PageSize=1&CSRF=1388959981-bedd066b4f6119c70465377bb8bc012a72ea7b13be683a0819b065aa9db559c7",
      "page_size": 1,
      "start": 0,
      "recordings": [
        {
          "sid": "RE54146168533b781c0cbbaa9ada009d29",
          "account_sid": "ACb76fd7689d66a37b3defd366bc3caf12",
          "call_sid": "CAd5396192caf4c5f73a45c5026b7156a5",
          "duration": "19",
          "date_created": "Fri, 03 Jan 2014 15:32:36 +0000",
          "api_version": "2010-04-01",
          "date_updated": "Fri, 03 Jan 2014 15:32:36 +0000",
          "uri": "/2010-04-01/Accounts/ACb76fd7689d66a37b3defd366bc3caf12/Recordings/RE54146168533b781c0cbbaa9ada009d29.json"
        }
      ],
      "next_page_uri": "/2010-04-01/Accounts/ACb76fd7689d66a37b3defd366bc3caf12/Recordings.json?AccountSid=ACb76fd7689d66a37b3defd366bc3caf12&Page=1&CSRF=1388959981-bedd066b4f6119c70465377bb8bc012a72ea7b13be683a0819b065aa9db559c7&PageSize=1&Format=json",
      "end": 0,
      "total": 178,
      "last_page_uri": "/2010-04-01/Accounts/ACb76fd7689d66a37b3defd366bc3caf12/Recordings.json?AccountSid=ACb76fd7689d66a37b3defd366bc3caf12&Page=177&CSRF=1388959981-bedd066b4f6119c70465377bb8bc012a72ea7b13be683a0819b065aa9db559c7&PageSize=1&Format=json",
      "page": 0
    }

Twilio控制器 -

@account_sid = @user.twilio_account_sid
@auth_token = @user.twilio_auth_token

# set up a client to talk to the Twilio REST API
@sub_account_client = Twilio::REST::Client.new(@account_sid, @auth_token)
@subaccount = @sub_account_client.account

@recordings = @subaccount.recordings

1 个答案:

答案 0 :(得分:0)

您的网址中可以有page个参数,“下一个”按钮是指向page参数递增的当前页面的链接。然后在服务器上发出请求

"/2010-04-01/Accounts/ACb76fd7689d66a37b3defd366bc3caf12/Recordings.json?AccountSid=ACb76fd7689d66a37b3defd366bc3caf12&Page=#{param[:page]}&CSRF=1388959981-bedd066b4f6119c70465377bb8bc012a72ea7b13be683a0819b065aa9db559c7&PageSize=1&Format=json"