How to do pagination across multiple tabs using Kaminari Gem - ruby-on-rails

I have a method which fetches results using solr. for eg.
def test
#result1 = Model1.search do
fulltext params[:search]
paginate page: params[:page], per_page: 12
end
#result2 = Model2.search do
fulltext params[:search]
paginate page: params[:page], per_page: 12
end
end
Now I have 2 results and I am displaying this in test page across 2 tabs.
When I display content via tabs, the data gets populated correctly in each tab, but I have a problem in pagination.
When I click suppose say on page number 3 in result2 tab it displays page3 of result2 but when I click on result1 tab it takes me to page3 of result1 tab. Where as it should go to page1 when I click on the result1 tab.
<%= paginate #result1 ,:params => { :anchor => 'result1' }%>
<%= paginate #result2 ,:params => { :anchor => 'result2' }%>
How can I solve this. I am using kaminari gem for pagination.

You can use the option param_name in kaminari like so
<%= paginate #result1, param_name: 'result1' %>
and likewise for #result2. Then instead of params[:anchor] you should use params[:result1] to get the page number. Since you'll be using different param names for #result1 and #result2, they won't interfere with each other

Related

will_paginate with 2 list on the same view

I have too list #comments and #eval in the same view. If I go to the page 2 in the #eval, the comments page go to the page 2 as well, there is a simple way to do this in the separated way?
You can define a custom parameter name (param_name) for each pagination link group and differentiate the page number by that:
# in view
<%= will_paginate #foos, :param_name => :foo_page %>
<%= will_paginate #bars, :param_name => :bar_page %>
# in controller
#foo = Foo.paginate(page: params[:foo_page])
#bar = Bar.paginate(page: params[:bar_page])
Find the list of possible options in the source code of the gem: https://github.com/mislav/will_paginate/blob/v3.0.5/lib/will_paginate/view_helpers.rb#L46

why doesn't will_paginate display the items after the first page? (it's null past page 1)

I am paginating an array #items
Here's the relevant controller part
class StaticPagesController < ApplicationController
def home
if signed_in?
#post = current_user.microposts.build
#activities = PublicActivity::Activity.order("created_at desc").paginate(page: params[:page])
#feed_items = current_user.feed.paginate(page: params[:page])
#items = (#feed_items + #activities)
#items.sort_by! {|item| -item.created_at.to_i}
#items = #items.paginate(:page => 1, :per_page => 10)
else
redirect_to '/signin'
end
end
Basically, this is the line that I'm using.
#items = #items.paginate(:page => 1, :per_page => 10)
I also tried changing that to the code below but didn't work.
#items = #items.paginate(page: params[:page], :per_page => 10)
Inside initializers\will_paginate_array_fix.rb
I have this line
require 'will_paginate/array'
In my view, I am using this
<%= render partial: 'shared/item', collection: #items %>
<%= will_paginate #items %>
It seems to work fine for the first page, but when I click on page 2 or 3 or others, I get a blank. It's null. Anyone know how I can fix this?
I think your problem is that you are building your array by combining activities and feed items, which are already paginated and then your are paginating it again.
For example assume that your activity and feed item models are using the will paginating default which is something like 20 items per page and that there are 15 activities and 12 feed items.
On page 1 you combine this array,take the top 10 and display them. #items had 27 entries so you show links to page 2 & 3. But when you click on that you're trying to load page 2 of activities and page 2 of feed items, which are empty since there are fewer than 20 of each.
In my experience trying to paginating through several collections like this is very tricky (unless you can load all of them and just paginate the array). A "load more items" button is easier because you just have to keep track of what the last cutoff date was

Will_paginate in Dynamic pages as "search"

I'm having trouble with will_paginate. It works perfectly with static pages but not in dynamic pages. In my controller I have:
def search
#prods = Prods.find_all_by_producer(params[:producer])
#items = #prods.paginate(:page => params[:page], :per_page => 10)
end
In my view:
<%= will_paginate #items %>
The first 10 items (the first page) are well displayed but when I try to navigate to next pages, I have:
undefined method `paginate' for nil:NilClass
Parameters : {"page"=>"2","locale"=>nil}
I understand the issue, there is no params[:producer] when it calls the second page so #prods returns nil. But how to do that, any ideas?
Add the #prods as param like this:
{:producer=>#producer} %>
Source: http://www.cowboycoded.com/2009/09/08/appending-parameters-on-a-will_paginate-link/

Paginate Multiple Models in Kaminari

I'm creating a search page that will do an application wide search on users, posts, and comments. I currently have:
# POST /search
def index
query = params[:query]
#users = User.search(query).page(params[:page])
#posts = Post.search(query).page(params[:page])
#comments = Comment.search(query).page(params[:page])
respond_to do |format|
format.html
end
end
However I'm really trying to get something where all the results are mixed together then paginated. What are some of the strategies for doing paginated search like this? Thanks!
Ever since this commit: https://github.com/amatsuda/kaminari/commit/f9f529fb68ab89feea38773a4c625c1b14859128
You can do the following
In your view you can do this:
<%= paginate #users, :remote => true, :param_name => "user_page" %>
<%= paginate #posts, :remote => true, :param_name => "post_page" %>
<%= paginate #comments, :remote => true, :param_name => "comment_#{some_post_id}_page" %>
and then in your controller you can refer to them in this way:
#users = User.search(query).page(params[:user_page])
#posts = Post.search(query).page(params[:post_page])
#comments = Comment.search(query).page(params[:comment_page])
and your view's js.erb you might have something like:
$('#posts').html('<%= escape_javascript render(#posts) %>');
$('.table-pager').html('<%= escape_javascript(paginate(#posts, :remote => true).to_s) %>');
Before thinking about a solution, you need to first define exactly what you want the final result to be. If you want to display a few of each type of record on the results page you can modify the approach you posted and combine the three paginated results using:
#results = #users + #posts + #comments
#results.sort! { |a, b| a.score(query) > b.score(query) }
Each object will need to have an instance method 'score' that will let it sort based on the query priority. Also, you will need to modify your view to handle correct rendering of each item and ensure that the pagination is called on the model with the most pages.
Alternatively, a more robust method would be to add a full-text search service (such as Index Tank, Web Solr, Thinking Sphinx). The technology for what's hot for these moves quickly, so do some research and find one that fits your needs. Example syntax for this would be something like:
User.multi_solr_search query, models: [Post, Comment]
You could combine the results from the query and run page on that.
users = User.search(query)
posts = Post.search(query)
comments = Comment.search(query)
#results = users + posts + comments
#results.page(params[:page])

Change number of elements per page with <%= will_paginate %>

I'm using the will_paginate gem. The default is 30 elements per page. How do I customize this?
If your controller is called User, you can do something like this in your controller:
#users = User.paginate :page => params[:page], :per_page => 10, :order => 'name ASC' This will show 10 results per page.
In your view:
<%= will_paginate #users %>
See the per_page option here:
https://github.com/mislav/will_paginate/wiki
It will allow you to change the number displayed per page, for anytime that model is paginated.
For a controller/action specific approach see Raunak's answer.

Resources