【问题标题】:Will_paginate for includes commentsWill_paginate 用于包含评论
【发布时间】:2017-12-27 16:05:03
【问题描述】:

我发现了很棒的 gem will_paginate !但我面临使用问题。我正在构建一个组>帖子> cmets 应用程序,因此在我的组显示页面中,我正在显示帖子及其 cmets。为了限制查询的数量,我使用了这样的包含方法:

组控制器:

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

所以我也想对我的 cmets 进行分页。你知道怎么做吗?

我的代码: Group_show =

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

还有我的帖子/_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) 中定义 @cmets 的方法,它会非常有用;)

【问题讨论】:

  • 我想知道将您的post.comments 限制为最近的 (3) 是否更有意义:post.comments.order(created_at: :desc).limit(3)。然后有一个按钮,向另一个端点执行 ajax 请求,如果用户需要,它将呈现剩余的请求......我认为这会更有效。
  • @BigRon,谢谢你的回答,我已经在我的模型评论中这样订购了。你能展示如何看起来像一个 ajax 请求来执行它吗?
  • 试试@posts = Post.where(group_id: @group.id).order(upd_at: desc).includes(:user, { comments: :user })

标签: ruby-on-rails ruby include will-paginate


【解决方案1】:

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

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/cmets.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();

文档here 解释了remote: true 用于ajax 请求。向下滚动到“3.1.2 link_to”部分,然后是控制器和 js.erb 视图的 5.1 部分。

【讨论】:

  • 非常感谢@BigRon 对此主题的解释。实现它的方法现在看起来更加清晰。我仍然有一些错误,但我可以自己解决。再次感谢,继续您的工作
猜你喜欢
  • 2016-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 2010-12-25
  • 1970-01-01
相关资源
最近更新 更多