ruby on rails - will_paginate gem using model associations -
i using will_paginate gem , attempting utilize using model associations. right now, have group , post models, each group has list of posts. however, when attempt chain these models using will_paginate, not work. yet test in terminal group.find(15).posts.order('created_at desc')
, works. not sure if missing or if has how i'm handling 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 |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 tried @posts = post.paginate(page: params[:page], per_page: 3).order('created_at desc')
doesn't seem work. list of posts not ordered in descending order nor grouped in threes. advice on how utilize gem when using multiple models?
so problem settings @posts
correctly, instead of using iterate on @group.posts
, here in views:
# posts/_index.html.erb <%= render 'posts/posts' %> ... <%= will_paginate @posts %> # posts/_posts.html.erb <% @group.posts.each |post| %> <%= render 'posts/post', post: post, group: @group %> <% end %>
instead this:
# posts/_posts.html.erb <% @posts.each |post| %> <%= render 'posts/post', post: post, group: @group %> <% end %>
note <%= will_paginate @posts %>
part only draws navigation controls. list of posts coming render posts/posts
.