Error with Kaminari pagination - ruby-on-rails

I would like to paginate my objects with the Kaminari pagination gem. I have this line in my controller:
#products = Product.order("id").find_all_by_id(params[:id])
That line in my view:
<%= paginate #products %>
And that line in my model:
paginates_per 20
When I open my page where my objects are supposed to be listed, I have this error message :
undefined method `current_page' for #<Array:0x2964690>
The exception is raised at my <%= paginate #products %> line.
I have already made a pagination for another project and it was working really great. Could someone help me please ?
Thank you !

Edit:
The problem is that find_all_by_* returns an array, not an ActiveRecord::Relation.
You can do something like this instead
#products = Product.order("id").where("id IN (?)", params[:id])
Also, you should probably have a .page(params[:page]) in there.

Related

method "where" does not work when switched from will_paginate to pagy gem in rails

My will_paginate code in controller:
#posts = Post.paginate(page: params[:page], per_page: 100).order('created_at DESC')
My pagy code that replaced it:
#pagy, #posts = pagy_countless(Post.order('created_at DESC'), items:5, link_extra: 'class="" style="color:#222222; margin:3px;"')
My view where the problem is:
<% #posts.where(user_id: current_user.friends).each do |post| %>
The error message:
undefined method `where' for #<Array:0x00007f94329d1f70>
Did you mean? when
What am I doing wrong? The previous will_paginate implementation worked in terms of showing all the posts of user's friends. I'm lost as to what changed in switching to pagy.
UPDATE:
So for the moment, I've solved this by moving the "where..." logic to controller. So:
#pagy, #posts = pagy_countless(Post.where(user_id: current_user.friends).order('created_at DESC'), items:5, link_extra: 'class="" style="color:#222222; margin:3px;"')
And now it works as before. But I'm still confused as to why I had to do this.
Be aware pagy_countless returns an array of items, instead of an ActiveRecord collection. That's why you can't use ActiveRecord methods on #posts (like where).
Follow my link to see that collection is transformed into array using to_a and then it is returned as second argument of pagy_countless: https://github.com/ddnexus/pagy/blob/bd88866ad6001ae1ef6ce63080d36ecff7bc5603/lib/pagy/extras/countless.rb#L29
Two comments:
1. You must use ActiveRecord filters before calling pagy_countless on your collection (eg: where clauses)
2. If you need to filter records after pagination (which does not make sense in most of the cases), you must use Array methods like select of reject as following #posts.select { |record| record.user.in?(current_user.friends) }

Rails - will paginate not displaying pages correctly

Have been following the wiki from the git page but unable to see the problem I am having.
will_paginate is showing the first 10 of the landlords correctly but will not render the next page correctly, the first page 'sticks' no matter which page I visit.
I have tried other threads with the similar issue but did not yield the correct result.
Question: What am I doing wrong? I have copied what I think is the key code to my issue.
I have a landlord class and in that class I have these lines ...
model..
class landlord
self.per_page = 10
default_scope order: 'landlords.name ASC'
controller..
class Landlords_controller
def index
#landlords = Landlord.paginate(page: params[:landlord]).search(params[:search])
end
and the view ...
landlords/index.html.erb
<% #landlords.each ..... %>
<% end %>
<%= will_paginate #landlords %>
Incorrect symbol in :
#landlords = Landlord.paginate(page: params[:landlord]).search(params[:search])
should be
#landlords = Landlord.paginate(page: params[:page]).search(params[:search])
I love mondays!
Or, use:
<%= will_paginate #landlords, param_name: 'landlord' %>
This is useful if you have more than one will_paginate on a page, if not, use the default 'page'

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/

Tire search and will_paginate - undefined method `offset'

After headaches with ThinkingSphinx and Solr/Sunspot, we're trying out ElasticSearch and Tire as our search back-end - but I've hit a problem.
Here's my search command in the controller:
#results = Item.search params[:search], :page => ( params[:page] || 1 ), :per_page => 20
And this is the problem section of the view:
<%= page_entries_info #results %>
The error message I'm getting is
undefined method `offset' for #<Tire::Results::Collection:0xa3f01b0>
but only when there is more than one page's worth of results. If there are less than 20 items returned, then they get shown fine.
The only similar reported issue I could find elsewhere was solved by passing the :page and :per_page parameters into the search function, but I'm already doing that, to no avail.
Tire has a Pagination module but it doesn't define offset. You could file an issue with them to add it, but in the meantime you can monkeypatch it in your app:
Tire::Results::Pagination.module_eval do
def offset
(#options[:per_page] || #options[:size] || 10 ).to_i * (current_page - 1)
end
end
in my testapp, results are paginated just fine, with will_paginate 3.0 and tire 0.3. I wasn't aware will_paginate needed the offset method.
I've added it, however, copying over the "lint" test from will_paginate specs: https://github.com/karmi/tire/commit/e0e7730. Should be part of the next release.

Rails 3 & Kaminari pagination problem

Ok so I have decided to use Kaminari for pagination in a rails 3 project. I have followed the video from RailsCasts http://railscasts.com/episodes/254-pagination-with-kaminari
All goes well up until the point or running the server.
controllers/stories_controller.rb
def index
#stories = Story.all
#pages = Story.page(params[:page]).per(3)
#stories = Story.search(params[:search])
end
views/stories/index.html.erb
<%= paginate #pages %>
When i start the server the index page in question displays all the stories from the DB and renders the pagination view showing (1 2 Next > Last ยป). What am I missing to get the pagination working?
I still can not understand your code. Why do you assign Story.all to #stories in the 1st line and overwrite the variable in the 3rd line?
Anyways, #stories will display "all the stories from the DB" because you're not calling the pagination method (.per) on #stories. The pagination links will show you the paginated counts because you're calling per method on #page variable and passing it to the helper.
I mean, you need to call .per on the relation before passing it to <%= paginate %> helper.
It's quite simple.
I guess you want to get results from your search, right?
Try
#stories = Story.search(params[:search]).page(params[:page]).per(3)
and something like:
<% #stories.each do |story| %>
<%= render story %>
<% end %>
<%= paginate #stories %>
in your view

Resources