Will_paginate, Ruby on Rails, conditions

280 Views Asked by At

I am using will_paginate for my Rails project. Depending on 'Post' variable its 'mediatype' attribute I use either memo or photo and show results in different parts of the page. Will_paginate paginates both sides of the website together. If I have done 10 posts: 1 memo, 9 posts of photos, and 1 memo and I want to show 5 results per page, I will see on the front page one memo instead of two, and 4 photos. I use the same variable post and I have two different attributes how to make separate paginations for them? Here is my code. In fact I would like to add 'load more' button. It might a bit too complicated, so I will be satisfied with simple version of pagination. Still if you know how to make 'load more' buttons for two parts of the website, I will much appreciate. Thank you.

INDEX VIEW

<% content_for :memo_content do %>

<%= will_paginate @posts %>

<% @posts.reverse_each do |post| %>
  <% if post.mediatype == "Memo" %>

          <%= post.mediatype %>:
          <%= post.title %>;

  <% end %>
 <% end %>
<% end %>


<% content_for :media_content do %>
<% @posts.reverse_each do |post| %>
  <% if post.mediatype == "Photo" %>

          <%= post.mediatype %>:
          <%= post.title %>;

  <% end %>
 <% end %>
<% end %>

POSTS_CONTROLLER

  def index
      @posts = Post.order(name: :asc).paginate(page: params[:page], per_page: 5)
  end

1

There are 1 best solutions below

0
nirav kanani On

you can get count manually in controller and pass this count to will_paginate

Like:

@posts = Post.order(name: :asc).paginate(page: params[:page], per_page: 5)

from @posts get manually for both memo and posts of photos and pass this count in view.

<%= will_paginate @memo_count, :renderer => BootstrapPagination::Rails  %>
<%= will_paginate @posts_of_photos_count, :renderer => BootstrapPagination::Rails  %>