Will_paginate gem is not installing - Rails 4 - ruby-on-rails

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.

Related

Kaminari - undefined local variable or method `page' for

I was using will_paginate for pagination on my site but as we're using Active Admin I decided to change it to kaminari to avoid any of the conflict issues between the two. I removed will_paginate from the gem file and added kaminari, restarted the rails server and ran bundle install but I'm getting errors which suggest it has not been installed properly:
undefined local variable or method `page' for <SearchController:0x007fd546587140>
Rails version: 5
Gemfile:
gem 'kaminari', '~> 0.16.3'
#gem 'will_paginate', '~>3.1.6'
Controller:
...
#properties = #properties.page(page[:params]).per(9)
Not sure what the issue is?
There is no object page in your controller...
#properties = #properties.page(page[:params]).per(9)
I think what you wanted to do was...
#properties = #properties.page(params[:page]).per(9)

Pagination issue: `map' : undefined method 'existent'

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

numbers_and_words gem not working - NoMethodError in Posts#index - undefined method `to_words' for 42:Fixnum

I would like to convert some numbers in words.
So I installed the numbers_and_words gem with "gem install numbers_and_words" -
I restarted the server, and tried to run this example from the Read.me in my index.html.erb:
<%= 42.to_words %>
but I get this error:
NoMethodError in Posts#index -
undefined method `to_words' for 42:Fixnum
I checked the gem documentation a few times, but I couldn't figure out, what I am missing.
This is my posts controller regarding index.
def index
#posts = Post.order("created_at desc")
#published = Post.where(draft:false).order("created_at desc")
#drafts = Post.where(draft:true).order("created_at desc")
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
What did I wrong? Did I forget something in the installation process?
I am quite new to rails, sorry if this is a trivial newbie question.
Thank you so much for your help! Really appreciated.
Installing a gem is not sufficient to make it available in a Rails project. You need to add it to the Gemfile so that you can manage the dependencies with Bundler.
Edit the 'Gemfile' and add the gem
gem 'numbers_and_words'
Then run bundle again to update the Gemfile.lock
$ bundler
This will make the gem available to the app. It will also autorequire the gem on boot, if the gem uses the standard naming conventions.
It seems this gem is name correctly. Otherwise, you can explicitly require it by setting a require option in the gemfile
gem 'numbers_and_words', require: 'numbers_and_words'
If you just installed the gem locally and you didn't configure the Gemfile, the application will crash once you will deploy it.
Found a solution - not mentioned in the documentation of the gem, but it did the trick for me:
I added
require 'numbers_and_words'
in the rake file. After server restart, it's working.

Why will_paginate not loaded in rails 3.2.8 engine?

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)

How to Install Kaminari Properly?

I'm having a bit problem on how to get Kaminari work. I did the installation procedure in GitHub.
gem "kaminari"
Then run
bundle
I have this snippet for index
#users = User.order("name")
I added this on my view
<%= paginate #users %>
Then I got this error.
undefined method `paginate' for #<#<Class:0x00000102934330>:0x00000102932508>
Did I missed something? I also tried to include the page method
#users = User.order("id").page(1)
But I get this error instead
undefined method `page' for #<ActiveRecord::Relation:0x000001017d0300>
include kaminari and bootstrap-kaminari-views gems in your project Gemfile,
gem "kaminari"
gem "bootstrap-kaminari-views"
Execute bundle install in terminal,
$ bundle install
In products_controller.rb,
#products = Product.order("name")
#products = Kaminari.paginate_array(#products).page(params[:page]).per(5)
In products/index.html.erb,<%= paginate #products, :theme => 'twitter-bootstrap-3' %>
take a look at the railscast for kaminari
its really nice
http://railscasts.com/episodes/254-pagination-with-kaminari
bash
rails g kaminari:views default
products_controller.rb
#products = Product.order("name").page(params[:page]).per(5)
products/index.html.erb
<%= paginate #products %>
app/views/kaminari/_prev_span.html.erb
<span class="prev disabled"><%= raw(t 'views.pagination.previous') %></span>

Resources