简体   繁体   English

使用模型关联将will_paginate gem

[英]will_paginate gem using model associations

I am using the will_paginate gem and I am attempting to utilize it using model associations. 我正在使用will_paginate gem,并且尝试通过模型关联来利用它。 Right now, I have Group and Post models, where each group has a list of posts. 现在,我有“组”和“帖子”模型,其中每个组都有一个帖子列表。 However, when I attempt to chain these models using will_paginate, it does not work. 但是,当我尝试使用will_paginate链接这些模型时,它不起作用。 Yet I test this in my terminal with Group.find(15).posts.order('created_at DESC') and it works. 但是我在终端中使用Group.find(15).posts.order('created_at DESC')并且可以正常工作。 Not sure if I am missing something or if it has to do with how I'm handling the partials. 不知道我是否缺少某些东西,或者是否与我处理零件有关。

Group show.html.erb 组show.html.erb

<%= render "posts/index" %> 

Post _index.html.erb 发布_index.html.erb

<%= render 'posts/posts' %>
...
<%= will_paginate @posts %>

Post _posts.html.erb 发布_posts.html.erb

<% @group.posts.each do |post| %>
  <%= render 'posts/post', post: post, group: @group %>
<% end %>

Post _post.html.erb 发布_post.html.erb

...
<%= post.caption %>

Post index.js.erb 发布index.js.erb

$('#posts').append("<%= escape_javascript(render 'posts')%>");
$('.pagination').replaceWith('<%= escape_javascript will_paginate(@posts) %>');

scroll.js scroll.js

$(document).ready(function() {
if ($('.pagination').length) {
$(window).scroll(function() {
  var url = $('.pagination .next_page').attr('href');
  if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
    $('.pagination').text("Please Wait...");
    return $.getScript(url);
  }
 });
 return $(window).scroll();
}
});

Group controller 组控制器

def show
    @group = Group.find(params[:id])
    @posts = @group.posts.paginate(page: params[:page], per_page: 3).order('created_at DESC')
end

I also tried @posts = Post.paginate(page: params[:page], per_page: 3).order('created_at DESC') but it doesn't seem to work. 我也尝试了@posts = Post.paginate(page: params[:page], per_page: 3).order('created_at DESC')但它似乎不起作用。 My list of posts are not ordered in descending order nor are grouped in threes. 我的帖子列表没有降序排列,也没有三列。 Any advice on how to utilize this gem when using multiple models? 有关在使用多个模型时如何利用此gem的任何建议?

according to the docs on the gem, the paginate call should be paginate(:page => params[:page], :per_page => 10) and not paginate(page: params[:page], per_page: 3) like you used in your code. 根据关于gem的文档,分页调用应为paginate(:page => params[:page], :per_page => 10)而不是paginate(page: params[:page], per_page: 3)在您的代码中。

That is where your problem may lie. 那就是你的问题所在。 If that doesn't fix it, you can also try: 如果仍不能解决问题,您也可以尝试:

def show
  @group = Group.find(params[:id])
  @posts = @group.posts.order('created_at DESC').paginate(:page => params[:page], :per_page => 3)
end

One thing I do when I use pagination in my applications is to paginate only after I am done getting the results of my query. 在应用程序中使用分页时,我要做的一件事是仅在完成查询结果后才进行分页。

So the problem is that you are settings up @posts correctly, but then instead of using it you iterate over @group.posts , here in the views: 因此,问题在于您正确设置了@posts ,但是随后您不使用它而是遍历@group.posts ,在视图中:

# posts/_index.html.erb
<%= render 'posts/posts' %>
...
<%= will_paginate @posts %>

# posts/_posts.html.erb 
<% @group.posts.each do |post| %>
  <%= render 'posts/post', post: post, group: @group %>
<% end %>

Instead do this: 而是这样做:

# posts/_posts.html.erb 
<% @posts.each do |post| %>
  <%= render 'posts/post', post: post, group: @group %>
<% end %>

Note that the <%= will_paginate @posts %> part only draws the navigation controls. 请注意, <%= will_paginate @posts %>部分绘制导航控件。 The list of posts is coming from the render posts/posts . 帖子列表来自render posts/posts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM