Rails - 如何使 Will-Paginate 链接显示在表格下方



我刚刚遇到了will-paginate宝石。我正在尝试将记录分成多个页面。我设法取得了一些进展,但我遇到了一个问题。

"分页"选项卡(显示页码)显示在表格上方。它也是不居中的。如何让选项卡显示在表格下方。

这就是它目前的样子

                          <= Previous 1 2 Next =>
                                 Table Data

我希望它看起来如何

                                 Table Data
                           <= Previous 1 2 Next =>

这是我的代码

<tbody>
  <% g = Game.where(console: @title.split(' ').first(2).join(' ')).order(title: :asc).paginate(:page => params[:page], :per_page => 2) %>
    <% g.each do |game| %>
    <tr>
      <td> <%= link_to "#{game.title}", game %></td>
      <td> <%= game.genre %></td>
      <td> <%= game.release_date %></td>
      <td> <%= game.rating %></td>
      <% if signed_in? && current_user.admin? %>
      <div>
        <td id = 'actions'><%= link_to 'Show', game, class: 'action' %></td>
        <td id = 'actions'><%= link_to 'Edit', edit_game_path(game), class: 'action' %></td>
        <td id = 'actions'><%= link_to 'Delete', game, method: :delete, class: 'action' %></td>
      </div>
      <% end %>
    </tr>
    <%= will_paginate g %>
  <% end %>
</tbody>

我什至把will_paginate放在代码块的末尾。

您的标记无效。您对will_paginate的调用位于表内,位于标记之间。

如果您希望它显示在下方、上方或其他任何地方,只需将呼叫分页放在那里即可。

<%= will_paginate collection %>

您确实应该在控制器中实例化实例变量。您认为不应看到此行。

<% g = Game.where(console: # ...

通过这种方式,您可以解决放置分页调用的位置的限制,并编写更多惯用的 Rails 代码。

如果你必须沿着这条路走下去,我强烈反对你,只要把你的收藏品放在桌子上面。

<% g = Game.where(console: @title.split(' ').first(2).join(' ')).order(title: :asc).paginate(:page => params[:page], :per_page => 2) %>

然后,在此之下,呈现分页,因为变量g现在存在:

<%= will_paginate g %>

然后呈现您的表格。

最新更新