will_paginate error in rails - ruby-on-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

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.

Pagination error in gem kaminari?

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

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 not working on records retrieved with find

I'm typing this in console:
user.bookmarks.page(1)
-> ok
Bookmark.find([21, 23]).page(1)
-> undefined method `page' for #<Array:0x5b2b3a0>
Also tried using the paginate method: Bookmark.find([21, 23]).paginate(page: "1") , with the same outcome.
Any idea what goes wrong in here?
Perhaps this:
Bookmark.paginate(:conditions => {:id => [21,23]}, :page => 1)
Or Simpler:
Bookmark.where(:id => [21,31]).page(1)
If you are using rails 3 or above, will_paginate/array needs to be loaded explicitly in config/application.rb to paginate array (with Bookmark.find or Bookmark.all)

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.

Resources