How to fix Kaminari paginate error in view? - ruby-on-rails

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.

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.

Rails 4 - will_paginate and mongodb

I'm getting a no method error when trying to implement pagination on my rails project.
undefined method 'paginate' for #<Mongoid::Criteria:0x007fb256e54588>
In my controller I have:
#user = User.all.paginate(:page => params[:page], :per_page => 10)
In my index.html.erb file I have <%= will_paginate #user %> above my html <table> tag and below the closing html </table> tag
I have the will_paginate and mongoid-pagination gems installed properly. And I have tried restarting the dev server.. Am I missing anything else?
You want to use the will_paginate_mongoid gem:
It just creates a paginate method compatible with will_paginate interface [...]
The mongoid-pagination gem requires you to manually include Mongoid::Pagination in your models and doesn't specifically integrate with the will_paginate gem.
The will_paginate gem has support for Mongoid out of the box.
require 'will_paginate/mongoid'
In your controller action
User.all.paginate(page: params[:page], per_page: 10)

Rails 4.2 will_paginate error

Trying to upgrade to Rails 4.2 from 4.1.8 and I'm getting a "wrong number of arguments (2 for 0..1)" for this line:
<%= will_paginate(#search) %>
Works perfectly find in Rails 4.1.8. #search is a custom object which defines the methods will_paginate needs (total_pages, etc).
The method signature for will_paginate is:
def will_paginate(collection = nil, options = {}) #:nodoc:
And I verified the proper method is being called by using:
<%= self.method(:will_paginate).source_location %>
Which outputted:
["/Users/home/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/will_paginate-3.0.7/lib/will_paginate/view_helpers/action_view.rb", 26]
Kind of stumped, and surprised that nobody else has encountered this issue.
Turns out the bartt-ssl_requirement gem overrides :url_for in a way that is not compatible with Rails 4.2. Removing that gem resolved the issue.

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 giving blank pages

I'm use will_paginate gem in my Rails application.
controller code:
def index
#posts = Guestbook.order('id DESC').page(params[:page]).per_page(10)
end
view code (used foundation gem):
<%= foundation_paginate #posts %>
Everything works fine, but if you type in the address bar room for a page, a blank page is displayed (no error messages or redirect to the first page). Tell me how to fix it, please.
P.S. I'm sorry for my bad english
you have to set will_paginate to use an array.
In config/initializers/
create a new file called will_paginate_extensions.rb
and add this:
require 'will_paginate/array'
I also would recommend not to use that foundation gem you are using for will_paginate, It can't receive parameters. I ran into that problem with that gem so i made one that can receive parameters
https://github.com/acrogenesis/will_paginate-foundation

Resources