Pagination error in gem kaminari? - ruby-on-rails

I am working on rails 4 and this are my steps:-
gem kaminari --in gemfile
bundle install
#vendors = Vendor.order(:name).page params[:page] in vendor_controller.rb)
also tried #vendors=Vendor.order("name").page(params[:page]).per(5)
<% = paginate #vendors %> in index.html.erb
Error:-
undefined method `page' for #<ActiveRecord::Relation::ActiveRecord_Relation_Vendor:0x16bec10>
I have also tried in console
Vendor.count
a = Vendor.limit(5).count
b = a.page(1).per(20).size
Error:-
NOmethoderror undefined method 'page'
question:-
Where i am going wrong .?
Thanks

Try calling the page method directly from the ActiveRecord class. You're calling it after order, which returns an ActiveRecord::Relation object.
#vendors = Vendor.page(params[:page]).order(:name)

I think this one may solve your problem:
vendor.except(:limit, :offset)
In model use:
paginates_per 5

Related

undefined method `paginate' despite having will_paginate gem

I'm a rails newbie and I'm trying to construct a feed of posts that are pulled from the database based on the characteristic of a category (in this case music). The action getting flagged is:
def music
#music_wads = Wad.find_by(category: "Music").paginate(page: params[:page])
end
Perhaps I'm making an error with the syntax, but I have the gem will_paginate installed. I have yet to actually add the <%= will_paginate #music_wads %> to my view however. Any thoughts?
Try .where instead of .find_by:
#music_wads = Wad.where(category: "Music").paginate(page: params[:page])
.find_by returns first record matching your conditions, while .paginate needs an ActiveRecord::Relation collection of records to work.

How to fix Kaminari paginate error in view?

When I use Kaminari gem with the code:
Model:
def index
#posts = Post.page(params[:page])
end
And use it in index.html.erb:
It prints the next error:
undefined method `paginate' for #<#<Class:0x000000029bbd68>:0x000000029b3dc0>
red line on <%= paginate #posts %>.
How can I fix this problem? Is my #posts paginable? I think yes.
If it's in the Gemfile, then you just need to run bundle install instead of gem install Kaminari. I would then restart my rails server to ensure its initializers are run to properly require its files.

will_paginate with search function is not working in ruby on rails

I am trying to search my posts item with pagination. I am doing like this..
#posts = Post.search(params[:search]).paginate(page: params[:page],:per_page => 5)
But it showing NoMethodError
undefined method `paginate' for #<Array:0x9a93f08>
I don't know where i am wrong. Please help
I just change my code
#posts = Post.search(params[:search]).paginate(page: params[:page],:per_page => 5)
to this
#posts = Post.paginate(page: params[:page],:per_page => 5).search(params[:search])
Now it works fine :)
params[:search]) gives you an array. And Array don't have paginate method, so errors thrown up. Use p params[:search]).inspect to see the same.
I will advise you to use Kaminari instead. As you can see by the Readme there's one section on to paginate Arrays.
Also make sure you read the Kaminari recipes where is an example on paginate Arrays.

will_paginate and having problems getting to work

I'm getting errors on will_paginate such as: undefined method `paginate' for # in my rails app.
I'm trying to do the simplest possible query such as:
#results = Location.paginate(:page => params[:page])
or
#results = Location.paginate(:page => params[:page]).order('name')
Location.order('name') works fine in console
Any ideas what is going on or next steps? Does anything need to be added to the model?
Have you configured everything as outlined in the docs? I remember when I first got started with rails and will_paginate, I had to restart the web server for everything to work.

will_paginate error in rails

Excuse me I have a problem with will_paginate plugin.
In localhost, in my controller, I use person.operations.descend_by_date.paginate :page => params[:page] || 1. However in production the method show error. I think that the problem is thye parameter page. Because person.operation.descend_by_date return a collection i try passing only params[:page] || 1.
However in localhost show error. The rare subject is:
If I evaluate the method once => Show error
IF i evaluate the method twice or more times => works ok
Rails show this message error: hash parameters expected
Why could be the error?
First i convert the will_paginate object to array and then i use the method for arrays of will_paginate
person.operations.descend_by_date.to_a.paginate params[:page] || 1
Thanks

Resources