我在發現gem will_paginate這很棒!但我正面臨使用問題。我正在構建一個羣組>發佈>評論應用,因此在我的羣組展示頁面中,我展示了帖子及其評論。爲了限制查詢的號碼,我使用的方法包括這樣的:Will_paginate for includes評論
Group_controller:
def show
@posts = @group.posts.order(upd_at: :desc).includes(:user).includes(comments: :user).paginate(page: params[:page], per_page: 10)
end
所以我想也分頁我的意見。你知道一個辦法嗎?
我的代碼: Group_show =
<h1>Groupe <%= @group.name %></h1>
<div class="post_list<%[email protected]%>">
<%= render @posts %>
</div>
<%= will_paginate @posts, renderer: BootstrapPagination::Rails %>
而且我的職位/彥博=
<% @comments = post.comments %>
<ul id="comment_list<%=post.id%>">
<%- if @comments.any? %>
<%= render @comments, post: post %>
<%= will_paginate @comments, renderer: BootstrapPagination::Rails %>
<% end %>
</ul>
順便說一句,如果你有直接的Groups_controller(節目)來定義@comments的方法,它可以是非常有用的;)
不知是否更有意義,簡單地限制你的'post.comments'到最近(3):'post.comments.order(created_at:DESC).limit(3)'。然後有一個按鈕,執行一個ajax請求到另一個端點,如果用戶需要它們,它將呈現剩餘的請求...我想這會更有效率。 – BigRon
@BigRon,謝謝你的回答,我已經把這個訂單放入了我的模型評論。你能展示如何看起來像ajax請求來執行該操作嗎? –
嘗試'@posts = Post.where(GROUP_ID:@ group.id).order(upd_at:DESC).includes(:用戶,{評論:用戶})' – Thanh