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)
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.
I'm using the google-api-client gem (gem google-api-client, '0.4.1' in my Gemfile). The following code caused the error uninitialized constant MyController::Google:
class MyController < ApplicationController
def index
#client = Google::APIClient.new
end
end
Specifying ::Google::APIClient didn't help, the error then said uninitialized constant Google.
Simply adding a require 'google/api_client' at the top of the file made this go away, so it seems there's something wrong in how auto-loading is being done. Not sure what's going on here exactly, specifying the gem in my Gemfile should have automatically required the gem, right? I have restarted the rails server btw.
Try adding a :require => 'google/api_client' where you specify the google api client gem in the Gemfile
gem 'google-api-client', :require => 'google/api_client'
This tells bundler that the correct way to require the gem 'google-api-client' is to require 'google/api_client'
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
]
I just updated all my gems and I'm finding that I'm getting errors when trying to run Test::Unit tests. I'm getting the error copied below. That comes from creating new, empty Rails project, scaffolding a simple model, and running rake test.
Tried Googling "uninitialized constant" and TestResultFailureSupport. The only thing I found was this bug report from 2007.
I'm using OS X.
These are the gems that I updated right before the tests stopped working:
$ sudo gem outdated
Password:
RedCloth (4.2.1 < 4.2.2)
RubyInline (3.8.1 < 3.8.2)
ZenTest (4.1.1 < 4.1.3)
bluecloth (2.0.4 < 2.0.5)
capistrano (2.5.5 < 2.5.8)
haml (2.0.9 < 2.2.1)
hoe (2.2.0 < 2.3.2)
json (1.1.6 < 1.1.7)
mocha (0.9.5 < 0.9.7)
rest-client (1.0.2 < 1.0.3)
thoughtbot-factory_girl (1.2.1 < 1.2.2)
thoughtbot-shoulda (2.10.1 < 2.10.2)
Has anyone else seen this issue? Any troubleshooting suggestions?
UPDATE
On a hunch I downgraded ZenTest from 4.1.3 back to 4.1.1 and now everything works again.
Still curious to know if anyone else has seen this or has any interesting comments or insights.
$ rake test
(in /Users/me/foo)
/usr/local/bin/ruby -I"lib:test" "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/helpers/users_helper_test.rb" "test/unit/user_test.rb"
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:105:in `const_missing': uninitialized constant Test::Unit::TestResult::TestResultFailureSupport (NameError)
from /usr/local/lib/ruby/gems/1.8/gems/test-unit-2.0.2/lib/test/unit/testresult.rb:28
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:158:in `require'
from /usr/local/lib/ruby/gems/1.8/gems/test-unit-2.0.2/lib/test/unit/ui/testrunnermediator.rb:9
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:158:in `require'
... 6 levels...
from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:214:in `run'
from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:12:in `run'
from /usr/local/lib/ruby/1.8/test/unit.rb:278
from /usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
/usr/local/bin/ruby -I"lib:test" "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/functional/users_controller_test.rb"
This can happen if modules are declared in a single statement when the parent module they are nested inside has not yet been loaded. I haven't looked at the code in those gems, but my hunch is that's what is happening. Chuck's solution would suggest that. calling gem 'test-unit' first will load the parent module, so the setup of zen test ends up working ok.
e.g.
module Foo::Bar
def do_stuff
# insert awesomeness here...
end
end
Will result in an error if the parent Foo module hasn't already been defined (e.g. by another gem)
A safer way to declare this is
module Foo
module Bar
def do_stuff
# insert awesomeness here...
end
end
end
May be a bug in Zentest that needs patching.
This apparently comes from using Test::Unit 2.0 with the old Test::Unit. According to Kouhei Sutou on RubyForge, it can be fixed by calling gem 'test-unit' before you require 'test/unit'.
As this link suggests http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/50
it may happen due to preliminary initialization of mocha lib.
To prevent it from happeing it is advisable to add line
config.gem 'test-unit', :lib => 'test/unit'
to config/environment.rb
Here is the recipe for test_unit 2.0.7 on Rails 2.3.5:
In config/environments/test.rb:
config.gem 'test-unit', :version => '2.0.7', :lib => false
In test_helper.rb,
add require 'test/unit',
immediately after require 'test_help'
So it looks like this:
require 'test_help'
require 'test/unit'
If you receive this error:
%': one hash required (ArgumentError),
upgrade gem i18n to v0.3.6.
I was getting this without mocha or shoulda installed.
This post suggests it's due to an incompatibility in test-unit >= 2.0, which I installed as a dependency for the systools gems. Downgrading to 1.2.3 worked for me, a simple require might also.
Same issue with me too.
Nothing above mentioned worked for me except downgrading test-unit back to 1.2.3
I'm missing the coloring of the test-unit 2.x
Found the (ugly) solution:
gem 'unit/test'
must be called inside the tests too, not only in the Rakefile.
I ran into this today on Mac OS X 10.6. My solution is as follows:
config.gem 'test-unit', :lib => 'test/unit', :version => '1.2.3'
config.gem 'autotest'
config.gem 'cucumber'
config.gem 'cucumber-rails', :lib => false
config.gem 'ffaker', :lib => 'faker'
config.gem 'rspec', :lib => false, :version => '>= 1.2.0'
config.gem 'rspec-rails', :lib => false, :version => '>= 1.2.0'
config.gem 'selenium-client', :lib => 'selenium'
config.gem "thoughtbot-factory_girl", :lib => 'factory_girl', :source => "http://gems.github.com"
config.gem 'thoughtbot-shoulda', :lib => 'shoulda'
config.gem 'webrat'
config.gem 'ZenTest', :lib => 'zentest'
As with aronchick's comment, for me (OS X 10.6) solution was
sudo gem uninstall test-unit
all versions.
You get auto_test to work again with something like
RUBY="ruby -I.:lib:test -rubygems -e 'gem \"test-unit\"'" autotest
I am not a nuby to rails but I am still learning and hopefully always will be :-).
Rails 2.3 production environment using Ruby Enterprise Edition and passenger can produce a totally misleading useless error during startup (/var/log/passenger.log). Something like:
Exception NameError in PhusionPassenger::Rack::ApplicationSpawner (uninitialized constant XXX)
If you run script/console on the production server, you may see:
Loading production environment (Rails 2.3.4)
/home/ubuntu/MyApp/shared/bundle/ruby/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:105:in const_missing:NameError: uninitialized constant XXX
This is happening to me in this instance only in production environment, not staging, not development. After a full day of research and experiments, I have concluded that in the production environment, REE or something must pre-load classes and the pre-loader apparently does not like to see classes being re-opened before they are created (an educated guess).
In app/models/calendar.rb:
Class Calendar < ActiveRecord::Base
# This defines the class
End
In app/models/event.rb
Class Event < ActiveRecord::Base
# This desined the class
End
Class Calendar
# This is supposed to 're-open' the Calendar class
has_many :events
end
The above generic sample code snippet can cause startup issue. I am not sure of the order that pre-loading classes takes place but I suspect that may be the issue. I moved the ‘has_many :events’ into the class definition in app/modeles/calendar.rb and my app now starts without error.
So, a good rule to follow is to put your Active Record Associations (has_many, belongs_to) inside the defining class (where the class is created).
If you add the following line to your environment.rb, or your config/environments/test.rb this should fix the issue
config.gem "test-unit", :lib => "test/unit", :version => ">=2.0.9", :env => "test"
I Believe you will need to add it before the mocha line if you use mocha.