Here is the code in our index to return #customers:
#customers = Customerx::Customer.where(:active => true).order("since_date DESC, id DESC").paginate(:per_page => 30, :page => params[:page])
It causes error of below:
undefined method `paginate' for #<ActiveRecord::Relation:0x6b88960>
In debug, ActiveRecord::Base.respond_to? :paginate returns false. It seems to us that the will_paginate was not loaded, even though will_paginate (3.0.3) was returned with gem list.
What could be wrong with the code?
The problem was fixed by adding gem will_paginate in engine's gemfile, even though gem will_paginate is in gemspec for the rails engine. The problem was caused by gem paginate was not loaded as ActiveRecord::Base.respond_to? :paginate returned false.
Here is a post that is somewhat similar to the problem here.
Rails Engine - Gems dependencies, how to load them into the application?
even I used to get this error, so I tried page method...use
#customers = Customerx::Customer.where(:active => true).order("since_date DESC, id DESC").page(params[:page]).per_page(30)
Related
I just installed the will_paginate, 3.0.7 and bootstrap-will_paginate, 0.0.10 gem and when i call the following to get my articles paginated
def index
#articles = Article.paginate(page: params[:page], per_page: 5)
end
I get the following error upon attempting to launch the localhost server
/Users/Jack/.rvm/gems/ruby-2.3.1#global/gems/activesupport-5.0.0.1/lib/active_support/i18n_railtie.rb:45:in `map': undefined method `existent' for #<String:0x007fd4a2bfa5d0> (NoMethodError)
Did you mean? extend
What's going wrong?
This was an issue with earlier version of will_paginate with Rails 5.
You need to update your will_paginate gem to use latest updated one which is:
gem 'will_paginate', '3.1.5'
This issue has been fixed with this merge:
https://github.com/mislav/will_paginate/pull/450
for something reason will_paginate is not installed.
i have put the line
gem 'will_paginate', '~>3.0.6'
in my gemfile,
i even ran "gem install will_paginate" in the console.
it says installed sucessfully but using the instructions to check if will_paginate is indeed install returns false nil
reference:
>> defined? WillPaginate
>> ActiveRecord::Base.respond_to? :paginate
If any of these lines return nil/false, will_paginate has not properly loaded in your app.
and i get this error when implemented
undefined method `total_pages' for #<Post::ActiveRecord_Relation:0x007fa40773bc18>
UPDATE:
I specifically stated in my gemfile to install version 3.0.5 not the latest version(3.0.7) and the gem is loaded properly. BUT the error with the total_pages still remains.
more info:
controller:
#posts = Post.paginate(:page => params[:page])
view:
<%= will_paginate #posts %>
UPDATE 2:
FIXED the error (had a typo) BUT i cannot see visually the gem anywhere on the page.
I am using Ransack simple search with kaminari for pagination successfully on rails 4.0.4. I have upgraded rails with this project to 4.1.0, which is the only change, and now if I search without entering any inputs in the search form I get: undefined method `delete_if' for "":String This previously worked with the earlier version of rails!
#q = HouseDesign.search(params[:q])
#house_designs = #q.result
#q.sorts = 'price asc' if #q.sorts.empty?
#house_designs = #house_designs.page params[:page]
url when it errors note
q=?&page=2&q=&utf8=✓ 4.1.0
url when successful
?&page=2&utf8=✓ 4.0.4
This is only my second project in rails so I could be missing something but I have spent all day searching to no avail.
Looks like this was fixed in this commit: https://github.com/activerecord-hackery/ransack/commit/71021b558b26d31e4b3b4a03cd595efd35ad25c4
Try running the rails-4.1 branch. It's working for me with Rails 4.1 with this in my Gemfile:
gem 'ransack', :github => "activerecord-hackery/ransack", :branch => "rails-4.1"
I've found a lot of links regarding this problem googling. but nevertheless I still get this error after implementing those fixes.
so my gemfile
gem 'will_paginate', '~> 2.3.16'
environment file
config.gem 'will_paginate', :version => '~> 2.3.16'
Controller
def index
#events = Event.paginate({:page => params[:page], :per_page => 10})
end
View:
<% #events.each do |item| %>
...
<% end %>
...
<%= will_paginate #events %>
I'm using rvm, console commands
rvm 1.8.7 do bundle install
...
rvm use 1.8.7 do bundle exec rake gems:install
Could anyone help me with this problem?
try this one
def index
#events = Event.all.paginate({:page => params[:page], :per_page => 10})
end
Note: If u get same error still, then just comment out logic of pagination and insert some records in your database table(model) and then use pagination.
You cannot call paginate on a model. It can be called on collections like hashes, array, ActiveRecord.
It may be just a case of restarting the server after you have installed the gem.
Newly installed will_paginate 3.0.0
gem 'will_paginate', '~> 3.0.0', :require=>'will_paginate/data_mapper'
Running a controller query:
#tickets = Ticket.paginate(:page => params[:page], :per_page => 5,:username => #ticket.username)
Which works, pulls up all the tickets for a user and paginates in 5's if I put ?page=X where x is a page number in the url.
=will_paginate(#tickets)
in the view does not work, this results in
undefined method `will_paginate' for #<#<Class:0x000000053674c8>:0x0000000535cd48>
So will_paginate works, but not the view helper. Am I missing something? I'm using slim templating if that makes any difference. Is there some syntax change I'm missing? The documentation is simple but unhelpful beyond this point. I looked into the source, and there does not seem to be any changes, but I cannot figure why it is inaccessible
And then on tangent, this messes with an association.
=> #instance.model_belonging_to_instance.create(:text=>'test')
TypeError: can't convert nil into Integer
from /home/qx/.rvm/gems/ruby-1.9.2-p180/gems/will_paginate-3.0.0/lib/will_paginate/page_number.rb:16:in `Integer'
etc etc et al
SOLUTION:
gemfile:
gem 'will_paginate', '~> 3.0.0' # removed this, :require=>'will_paginate/data_mapper'
intializer:
require 'will_paginate'
require 'will_paginate/data_mapper'
It shows up, but if not at the top of the template, I get a
stack level too deep
error I am unable to interpret
Don't use the :require option in the Gemfile, as you already figured out; instead require "will_paginate/data_mapper" somewhere in config/application.rb, for instance after the Bundler setup.
There is a similar question with an answer that indicates that auto-requiring here is the problem. See will_paginate undefined method. The Will_paginate gem appears to work though for the question and answer.
gem 'will_paginate', '~> 3.0.0', require: %w[
will_paginate
will_paginate/data_mapper
]