Will_paginate包括评论
我发现gemwill_paginate很棒! 但我面临使用问题。 我正在建立一个群组>发布>评论应用,所以在我的群组展示页面中,我正在显示post及其评论。 为了限制查询数量,我正在使用包含这样的方法:
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 =
Groupe
<div class="post_list">
没有100%测试,但我认为这应该工作。 你知道所有这些组件的工作原理吗? 如果没有,请告诉我,我可以解释一下。
文章/ _post
<% @comments = post.comments.order(created_at: :desc).limit(3) %>
配置/ 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
意见/职位/ 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”部分,然后向下部分转到控制器和js.erb视图。