rails server works but "rails console" not working - ruby-on-rails

I'm working on a rails project. With command "bundle exec rails s" I can fire up a local server, however, "bundle exec rails c" throws the following errors:
/Users/wh026399/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- bundler/setup (LoadError)
from /Users/wh026399/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/wh026399/healthelife_web/vendor/bundle/gems/spring-1.6.2/lib/spring/commands.rb:33:in `<module:Spring>'
from /Users/wh026399/healthelife_web/vendor/bundle/gems/spring-1.6.2/lib/spring/commands.rb:4:in `<top (required)>'
from /Users/wh026399/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/wh026399/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/wh026399/healthelife_web/vendor/bundle/gems/spring-1.6.2/lib/spring/application.rb:77:in `preload'
from /Users/wh026399/healthelife_web/vendor/bundle/gems/spring-1.6.2/lib/spring/application.rb:143:in `serve'
from /Users/wh026399/healthelife_web/vendor/bundle/gems/spring-1.6.2/lib/spring/application.rb:131:in `block in run'
from /Users/wh026399/healthelife_web/vendor/bundle/gems/spring-1.6.2/lib/spring/application.rb:125:in `loop'
from /Users/wh026399/healthelife_web/vendor/bundle/gems/spring-1.6.2/lib/spring/application.rb:125:in `run'
from /Users/wh026399/healthelife_web/vendor/bundle/gems/spring-1.6.2/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/wh026399/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/wh026399/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
Ruby version: 2.2.2
Rails version: 4.2.5
gem version: 2.5.2
Bundler version: 1.11.2
[UPDATE]: it turns out that commenting out "spring" in Gemfile will solve this issue.

Okay, I came across this after having the same problem as poster. His update suggesting that removing spring from the Gemfile helped me get on the right track. The problem for me was that I had introduced a new environment variables, but spring didn't pick it up. When you remove spring from the Gemfile, it prevents your app from using the spring process, but it doesn't actually stop the spring process so adding it back, doing bundle install, or anything else will still leave you with the same spring process running.
To see if this is likely your issue, you can check bin/spring status and see if spring is running in the background, and if it has been running for a while.
Solution (at least for me): restart spring
bin/spring stop
rails c

Have you tried gem update --system? You may need an updated version of rubygems.

if you use rvm set the default ruby version using following, so it will pickup the correct ruby version. you probably needs to run bundle install/gem install rails -v 4.2.5 after setting the default
rvm --default 2.2.2

Related

Rails + Big Sur + pg and Spring conflict?

Once I upgraded to Big Sur, I immediately got the following error in Ruby on Rails when launching the server:
$ rails s
/Users/carsoncole/.rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/x86_64-darwin19/stringio.bundle: warning: already initialized constant StringIO::VERSION
You've tried to invoke Spring when it's already loaded (i.e. the Spring constant is defined).
This is probably because you generated binstubs with Spring 1.0, and you now have a Spring version > 1.0 on your system. To solve this, upgrade your bundle to the latest Spring version and then run `bundle exec spring binstub --all` to regenerate your binstubs. This is a one-time step necessary to upgrade from 1.0 to 1.1.
Here's the backtrace:
/Users/carsoncole/.rvm/gems/ruby-2.7.1/gems/bootsnap 1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
/Users/carsoncole/.rvm/gems/ruby-2.7.1/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
/Users/carsoncole/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:318:in `block in load'
/Users/carsoncole/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency'
/Users/carsoncole/.rvm/gems/ruby-2.7.1/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:318:in `load'
/Users/carsoncole/.rvm/gems/ruby-2.7.1/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<main>'
/Users/carsoncole/.rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
/Users/carsoncole/.rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
/Users/carsoncole/scout_activity/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
I followed the directions given above but to no avail. Any thoughts?
If I create a new Rails app, the server starts up fine. Using Rails 6.0.3.4 and Ruby 2.7.3
One difference between the new app and my existing app is the new app did not use 'pg'. When I added 'pg' (pg-1.2.3) to the new app, the same error occurs.
[UPDATE] I think the issue may be related to Homebrew. I'm now reinstalling.
I found a solution, although I can’t pinpoint exactly why it worked.
I reinstalled Homebrew, and Posgresql (with HB). I also updated Xcode and the command line libraries. In the process of reinstalling homebrew and Xcode, I believe a number of libraries were updated/installed which I suspect was the issue.
Works fine now.

How to repair ruby on rails on mac

I have ruby on rails installed and something kinda went fubar on it. When I try to generate a controller, I get the following:
➜ librius rails generate controller Teachers
Running via Spring preloader in process 27015
/Users/erick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `load': no implicit conversion of nil into String (TypeError)
from /Users/erick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `block in load'
from /Users/erick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in `load_dependency'
from /Users/erick/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `load'
from /Users/erick/.rvm/rubies/ruby-2.4.0/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/erick/.rvm/rubies/ruby-2.4.0/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
➜ librius
I've tried to uninstall/reinstall using gem and RVM but nothing seems to work.
Thanks.
The comments helped point in the right direction.
I did a gem upgrade (which I had to download because it kept telling me I had the latest). Then I did gem pristine --all, then spring stop. (Still wasn't working.) Then lastly, bundle install and voila! Such a hassle! Thanks!

Rails console in production: NameError: uninitialized constant

I have a rails app (rails 5). In development, everything work, when i use
rails console
And enter an instruction, for example User.all , it's working.
In production, my app work perfectly, no problems, no errors, but when i use rails console production and enter for example User.all i have an error :
NameError: uninitialized constant User
from (irb):2
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/alexandre/tcheen/bin/rails:9:in `<top (required)>'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `load'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `call'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in `call'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:191:in `block in serve'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `fork'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `serve'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
I have the same problem with all my classes, but i repeat, the application work perfectly.
I develop on Mac OS and the app run in production on debian 8.
My models are correctly named i verified.
Thanks
I had this problem and realized it happened after I made a tweak to one of my job files.
What fixed it was restarting the spring loader. Just run
spring stop
Then the next time you run the rails console it should load things as normal.
I had this same issue, rewolf's answer above solved it for me temporarily.
Just to add to his answer:
After stopping the spring gem by running the command below
spring stop
You can also fix this issue permanently by upspringing (removing the spring gem from) your bin/ executables:
bin/spring binstub --remove --all
Or
spring binstub --remove --all
You can now run the command below to get into rails console in production
rails c --environment=production
Also, to avoid this from occurring in subsequent occasions endeavour to make sure that the spring gem is only present in development and test groups in your Gemfile.
Moreso, when you're in production make sure you always provide the --without development test argument to the bundle install command
bundle install --without development test
and not the usual or common
bundle install
Please note: As an indication, whenever you run the command rails c or rails console and you see the output below:
Running via Spring preloader in process 26651
WARNING: Spring is running in production. To fix this make sure the spring gem is only present in development and test groups in your Gemfile and make sure you always use bundle install --without development test in production
It's an indication that the spring gem is running in your production environment, and it should be stopped or removed entirely from your bin executables.
That's all.
I hope this helps

rails console on openshift

I deployed my rails application to openshift it works well but I cannot run 'rails console' on production server. It gives me this error. How can I solve this? I tried to update rubygems but it also gives permission denied error and I couldn't make it too.
rails c error:
Warning: You're using Rubygems 1.8.24 with Spring. Upgrade to at least Rubygems 2.1.0 and run `gem pristine --all` for better startup performance.
/opt/rh/ruby193/root/usr/share/rubygems/rubygems/custom_require.rb:36:in `require': cannot load such file -- bundler/setup (LoadError)
from /opt/rh/ruby193/root/usr/share/rubygems/rubygems/custom_require.rb:36:in `require'
from /var/lib/openshift/5429e72a500446dfee000197/app-root/runtime/repo/vendor/bundle/ruby/1.9.1/gems/spring-1.1.3/lib/spring/commands.rb:33:in `<module:Spring>'
from /var/lib/openshift/5429e72a500446dfee000197/app-root/runtime/repo/vendor/bundle/ruby/1.9.1/gems/spring-1.1.3/lib/spring/commands.rb:4:in `<top (required)>'
from /opt/rh/ruby193/root/usr/share/rubygems/rubygems/custom_require.rb:36:in `require'
from /opt/rh/ruby193/root/usr/share/rubygems/rubygems/custom_require.rb:36:in `require'
from /var/lib/openshift/5429e72a500446dfee000197/app-root/runtime/repo/vendor/bundle/ruby/1.9.1/gems/spring-1.1.3/lib/spring/application.rb:77:in `preload'
from /var/lib/openshift/5429e72a500446dfee000197/app-root/runtime/repo/vendor/bundle/ruby/1.9.1/gems/spring-1.1.3/lib/spring/application.rb:140:in `serve'
from /var/lib/openshift/5429e72a500446dfee000197/app-root/runtime/repo/vendor/bundle/ruby/1.9.1/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /var/lib/openshift/5429e72a500446dfee000197/app-root/runtime/repo/vendor/bundle/ruby/1.9.1/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /var/lib/openshift/5429e72a500446dfee000197/app-root/runtime/repo/vendor/bundle/ruby/1.9.1/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /var/lib/openshift/5429e72a500446dfee000197/app-root/runtime/repo/vendor/bundle/ruby/1.9.1/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /opt/rh/ruby193/root/usr/share/rubygems/rubygems/custom_require.rb:36:in `require'
from /opt/rh/ruby193/root/usr/share/rubygems/rubygems/custom_require.rb:36:in `require'
from -e:1:in `<main>'
rubygems-update error:
[appname.rhcloud.com repo]\> update_rubygems
ERROR: While executing gem ... (Errno::EACCES)
Permission denied - /opt/rh/ruby193/root/usr/local/share/ruby
I ran into the same issue, try running it like this:
RAILS_ENV=production bundle exec rails console
Also make sure you are running it in ~/app-root/repo when you run it
To update rubygems:
gem update --system
If that doesn't solve the problem try Spring's suggestion and run:
gem pristine --all
The problem is that there are multiple bundle executables and the system is using the wrong one.
Locate the correct bundle executable. In my case it was located at ~/.gem/bin/bundle.
If you can't find it, install it gem install bundler.
Then run ~/.gem/bin/bundle exec rails console production inside ~/app-root/repo
I couldn't get any of the gem install or bundle exec solutions to work while running the 4.1.4 Rails cartridge on OpenShift. My solution was running RAILS_ENV=production bin/rails c in app-root/repo.

Rails installation can't find active_model

I created a fresh project using rails new Project -d=postgresql. If I then run rails server, I get the following error message:
C:/GemRepository/gems/actionpack-3.1.3/lib/action_dispatch.rb:34:in `require': cannot load such file -- active_model (LoadError)
from C:/GemRepository/gems/actionpack-3.1.3/lib/action_dispatch.rb:34:in `<top (required)>'
from C:/GemRepository/gems/railties-3.1.3/lib/rails/commands/server.rb:3:in `require'
from C:/GemRepository/gems/railties-3.1.3/lib/rails/commands/server.rb:3:in `<top (required)>'
from C:/GemRepository/gems/railties-3.1.3/lib/rails/commands.rb:48:in `require'
from C:/GemRepository/gems/railties-3.1.3/lib/rails/commands.rb:48:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I also tried
gem which active_model
which returned
C:/GemRepository/gems/activemodel-3.1.1/lib/active_model.rb
Please help me.
Had this problem, came across this post. This fixed it for me:
rvm gemset pristine
Well, I managed to solve it.
I restarted my computer, deleted everything from my gem repository, reinstalled rubygems (ruby setup.rb) and reinstalled rails (gem install rails).
Thanks anyway :)
What version of rails is in the gem file? And have you run 'bundle install'?
I notice that Actionpack is 3.1.3 is called by the server, but the activemodel found is 3.1.1.
Run the bundle command from the project directory
I had a similar error and it was due to accidentally corrupting my Gemfile, so it is worth checking that. If it is version controlled then just git checkout Gemfile and bundle update to rebuild.
gem uninstall active-model and perfomr gem install active-model

Resources