简体   繁体   中英

will_paginate gem using model associations

I am using the will_paginate gem and I am attempting to utilize it using model associations. 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. Yet I test this in my terminal with Group.find(15).posts.order('created_at DESC') and it works. Not sure if I am missing something or if it has to do with how I'm handling the partials.

Group show.html.erb

<%= render "posts/index" %> 

Post _index.html.erb

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

Post _posts.html.erb

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

Post _post.html.erb

...
<%= post.caption %>

Post index.js.erb

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

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. 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?

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.

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/_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. The list of posts is coming from the render posts/posts .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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