Will_paginate包括评论 [英] Will_paginate for includes comments

查看:127
本文介绍了Will_paginate包括评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现宝石will_paginate很棒!但我面临使用问题。我正在建立一个群组>发布>评论应用,所以在我的群组展示页面中,我正在显示帖子及其评论。为了限制查询的数量,我正在使用包含这样的方法:

I'm discovering the gem will_paginate which is great ! But I'm facing a problem of using. I'm building a group>post>comments app, so in my group show page i'm displaying posts and their comments. To limit the numbers of queries, i'm using includes method like this :

Group_controller:

Group_controller :

  def show
    @posts = @group.posts.order(upd_at: :desc).includes(:user).includes(comments: :user).paginate(page: params[:page], per_page: 10)
  end

所以我想对我的评论进行分页。你知道办法吗?

So I would like to also paginate my comments. Do you know a way to do that ?

我的代码:
Group_show =

My code : Group_show =

<h1>Groupe <%= @group.name %></h1>
<div class="post_list<%=@group.id%>">
  <%= render @posts %>
</div>
<%= will_paginate @posts, renderer: BootstrapPagination::Rails %>

我的帖子/ _post =

And my posts/_post =

<% @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(show)中定义@comments,那么可能真的很有用;)

By the way if you have a method to define @comments directly in the Groups_controller(show), it can be really useful ;)

推荐答案

没有100%经过测试,但我认为这应该有效。你知道所有这些组件的工作原理吗?如果没有,请告诉我,我可以解释一下。

Not 100% tested, but I think this should work. Do you know how all these components work? If not, let me know and I can explain.

posts / _post

<% @comments = post.comments.order(created_at: :desc).limit(3) %>
<ul id="comment_list<%=post.id%>">
  <%- if @comments.any? %>
    <%= render @comments, post: post %>
    <%- if post.comments.offset(3).exists? # this is more efficient than count > 3 bc it quits counting after 3 %>
      <!-- the below link_to creates: href="/posts/:id/comments" ... -->
      <!-- ... and `remote: true` makes that an ajax request -->
      <li><%= link_to "more", comments_post_path(post), class: "more-comments-btn", remote: true %></li>
    <% end %>
  <% end %>

</ul>

config / routes.rb

resources :posts do
  # `member do` is explained here: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
  member do
    get :comments
  end
end

posts_controller.rb

# GET /posts/:id/comments
def comments
  @post = Post.find(params[:id])
  @comments = @post.comments.order(created_at: :desc)
  # since you requested this url via ajax with `remote: true` rails will automatically render `posts/comments.js.erb` ...
  # ... rather than a typical html request where rails would automatically render `posts/comments.html.erb`
end

views / posts / comments.js.erb

// some people like to use render @comments as shorthand like you did above. I'm a fan of being more explicit like the below
$("#comment_list<%= @post.id %>").html("<%= escape_javascript(render partial: 'comments/comments', locals: {comments: @comments, post: @post}) %>");
// now remove the more comments button
$("#comment_list<%= @post.id %>").find(".more-comments-btn").remove();

文档这里解释了对ajax请求使用 remote:true 。向下滚动到3.1.2 link_to部分,然后转到5.1节的控制器和js.erb视图。

The documentation here explains the use of remote: true for ajax requests. Scroll down to section "3.1.2 link_to" and then section 5.1 for the controller and js.erb view.

这篇关于Will_paginate包括评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆