Cannot load Rails due to Switch to inspect mode error - ruby-on-rails

I'm on a fresh install of Ubuntu 14.04 LTS. RVM 1.26.11 is installed with Ruby ruby 2.2.0p0. I'm using envconsul to handle environment variables. However, when I do:
envconsul -config=/etc/envconsul.hcl bundle exec rails c
it returns this error:
I, [2015-09-02T00:10:00.420551 #3953] INFO -- : ** [Raven] Raven 0.14.0 ready to catch errors
Loading development environment (Rails 4.2.3)
Switch to inspect mode.
and just dumps out to the command line. I can run bundle exec rails c on its own, but it fails out because the environment variables aren't set. I tried setting an ~/.irbrc but that doesn't help at all.
I'm really stumped on this one. Anyone have any ideas?

try following:
export $(envconsul -consul your_consul_server/agent -prefix your_app_env/ -upcase -pristine -once env)
and then:
ruby -S bundle exec /usr/local/bin/rails c
you can obviously include all these envconsul options in your *.hcl file.

Related

Cannot start rails console unless spring is disabled

My rails development environment runs in Docker, and I cannot start the rails console without disabling spring using DISABLE_SPRING=true. The snipper below illustrates what happens:
app#docker:[project] $ bundle exec rails c
Could not find rake-13.0.6 in any of the sources
Run `bundle install` to install missing gems.
app#docker:[project] $ DISABLE_SPRING=true bundle exec rails c
D, [2022-01-19T03:44:17.663136 #20] DEBUG -- sentry: initialized a background worker with 6 threads
Loading development environment (Rails 6.1.4.4)
irb(main):001:0>
I've tried rebuilding my docker image from scratch, deleting the Gemfile.lock and building again. Nothing works, except for disabling spring.
Surprisingly, rails server works fine.
Any ideas?

Is there a way to connect to a Rails app's console on a linux server?

I have deployed a rails application using capistrano on a linux server and it is running without any problems. When I connect to my remote server via ssh, the folder structure of my app is quite different from what I have on my local machine. I would like to go to project root and say rails console so that I can have access to the console of my application. Is there a way to achieve this?
When I go to ~MyApp/ folder and run rails console it says command rails not found. I think that is probably because the app is running in another folder.
bundle exec to the rescue inside of project folded:
$ RAILS_ENV=production bundle exec rails console
If you've installed with rbenv it could be that you are defaulting to the wrong Ruby version. So you can run rbenv exec to get it to run on the right version, along with bundle exec to run the right version of Rails
So try running this:
rbenv exec bundle exec rails console
To sum up for anyone having the same problem in the future,
I went to MyApp/releases/2020331231231231 which is the latest release of my app and there I used this command RAILS_ENV=production bundle exec rails console and I was able to do whatever I wanted to do in the console.
First you need to move inside the folder where the code is.
Check Capistrano config if deploy_to is defined. (If it's not it should be set to /var/www/#{application}/ by default read here)
Since Capistrano keep older versions of your app go to the current folder which is a symlink of the latest deployed version.
And run ENVIRONMENT=production bundle exec rails c or ENVIRONMENT=production bin/rails c. If doesn't work try with rbenv ENVIRONMENT=production rbenv exec bundle exec rails c

Command: rails console, generate not recognize. Error: Command 'rails' not recognized Usage: rails COMMAND [ARGS]

When I go to rails folder (I'm inside my rails app folder) and type "rails c" or "rails g" it said:
Error: Command 'rails' not recognized
Usage: rails COMMAND [ARGS]
Usage: spring COMMAND [ARGS]
Commands for spring itself:
binstub Generate spring based binstubs. Use --all to generate a binstub for all known commands. Use --remove to revert.
help Print available commands.
server Explicitly start a Spring server in the foreground
status Show current status.
stop Stop all spring processes for this project.
Commands for your application:
rails Run a rails command. The following sub commands will use spring: console, runner, generate, destroy, test.
rake Runs the rake command
Error: Command 'rails' not recognized
Usage: rails COMMAND [ARGS]
The most common rails commands are:
generate Generate new code (short-cut alias: "g")
console Start the Rails console (short-cut alias: "c")
server Start the Rails server (short-cut alias: "s")
dbconsole Start a console for the database specified in config/database.yml
(short-cut alias: "db")
new Create a new Rails application. "rails new my_app" creates a
new application called MyApp in "./my_app"
In addition to those, there are:
destroy Undo code generated with "generate" (short-cut alias: "d")
plugin new Generates skeleton for developing a Rails plugin
runner Run a piece of code in the application environment (short-cut alias: "r")
All commands can be run with -h (or --help) for more information.
I'm running Linux Subsystem for Windows 10, I know I know you see Windows but it's kind of... Linux, and I installed Ruby by rbenv through this tutorial.
Try this command from your app folder:
rake rails:update:bin
Say y to replace the binaries.
I had the same problem using rails with Windows Linux Subsystem. I found the suggestion here:
Rails command Error
I tried it and it solved my problem, so maybe yours as well.
Even though the accepted answer solved your issue, I want to add another because I ran into the same error message but the underlying issue and my environment setup wasn't the same. However, recreating the executables in my app's bin folder did not solve the problem.
The issue: When I tried running bundle exec rails s or bundle exec rails c I got the same error as posted in the question. Using the apps executables worked fine though ./bin/rails s.
So the main differences from the question: I was using bundle exec when I got the error.
What was happening behind the scenes: bundle exec rails s was calling this executable /Users/robin/.rvm/gems/ruby-2.3.8/bin/rails which is shipped by the railties gem. I found this file was overwritten due to a gem which had an executable rails defined in its .gemspec.
What solved my problem:
Running $ bundle pristine railties (https://bundler.io/v2.0/man/bundle-pristine.1.html) so railties would be reset and with that the executable shipped by railties would overwrite the wrong one.
Addition: Of course it only helps as long as the intrusive gem shipping a rails executable is also updated and no longer wants it to be installed.

Cannot access Rails console in Windows - LoadError

I'm using JRuby because it's better for SRS Ruby on Rails, on Windows.
My solution was to use jirb in cmd.exe or Powershell (not gitbash).
I've tried:
$ rails console
Loading development environment (Rails 4.2.4)
Switch to inspect mode.
LoadError: load error: rails/commands -- java.lang.UnsatisfiedLinkError: The operation completed successfully.
require at org/jruby/RubyKernel.java:939
<top> at bin/rails:4
and:
$ bundle exec rails console
uri:classloader:/jruby/kernel/kernel.rb:17: warning: unsupported exec option: close_others
Loading development environment (Rails 4.2.4)
Switch to inspect mode.
LoadError: load error: rails/commands -- java.lang.UnsatisfiedLinkError: The operation completed successfully.
require at org/jruby/RubyKernel.java:939
<top> at bin/rails:4
and:
$ jruby.exe -S bundle exec rails console
uri:classloader:/jruby/kernel/kernel.rb:17: warning: unsupported exec option: close_others
Loading development environment (Rails 4.2.4)
Switch to inspect mode.
LoadError: load error: rails/commands -- java.lang.UnsatisfiedLinkError: The operation completed successfully.
require at org/jruby/RubyKernel.java:939
<top> at bin/rails:4
I tried the solution "Load error when running rails console", but restarting the computer has no effect. bin/spring has no mention of GEM_HOME. I reinstalled Rails and Spring.
Is this because of JRuby?
I'm entertaining the idea that jruby -S rails console requires C extensions. I found another way to access the console using JRuby's own jirc. I started with:
$ jirb
Switch to inspect mode.
then edited ~/.irbrc to include
IRB.conf[:PROMPT_MODE] = :SIMPLE
which produced:
$ jirb
Switch to inspect mode.
>>
which then immediately exits. Doing this:
$ jirb puts 'hello'
Switch to inspect mode.
Errno::ENOENT: No such file or directory - puts
initialize at org/jruby/RubyFile.java:342
open at org/jruby/RubyIO.java:1124
open at G:/jruby-9.0.1.0/lib/ruby/stdlib/irb/magic-file.rb:7
initialize at G:/jruby-9.0.1.0/lib/ruby/stdlib/irb/input-method.rb:100
initialize at G:/jruby-9.0.1.0/lib/ruby/stdlib/irb/context.rb:84
initialize at G:/jruby-9.0.1.0/lib/ruby/stdlib/irb.rb:426
start at G:/jruby-9.0.1.0/lib/ruby/stdlib/irb.rb:381
<top> at G:/jruby-9.0.1.0/bin/jirb:13
Since my console is still not working and nothing about jirc is using C extensions, the question is not a duplicate to that one.
Yes, I would try and install jruby to resolve this
See http://jruby.org/getting-started
Then do bundle from the root of the project
TL;DR use jirb in Command Prompt to execute Ruby code.
I never resolved making rails console work. This may have to do with my ruby installation being a part of jRuby.
However, irb and jirb allow me to execute Ruby snippets on the command line with either Command Prompt or Powershell. GitBash doesn't work, says Switch to inspect mode. followed by >> and then exits.

Capistrano deploy fails with rake run (rails3 + rvm)

EDIT:
The issue here was that I used RVM on my local machine and rbenv on the server. This is highly not recommended, if you're managing several ruby installation (or upgrading your ruby version but do not want to change the system's) use either RVM or rbenv on all environments!
/EDIT
So I'll start with a bit history:
I'm in the process of upgrading our rails2 website to rails3. Most of the things are working properly by now, except Capistrano's deploy script.
The current setup I have is:
RVM (1.14.1) installed locally
ruby-1.9.3-p194 (set to the project's folder)
Capistrano v2.12.0 (upgraded from 2.6.0, but it doesn't work on 2.6.0 as well) installed as an RVM gem
rvm-capistrano (1.2.2), added to the Gemfile (after doing some reading online and on SO)
rbenv (with ruby 1.9.3p194 used globally) installed on the server
(by now if you see anything that doesn't make sense, please let me know)
Nothing changed in the deploy script, yet when I try to deploy to my testing server (let's call it beta) I get this error:
* executing `deploy:restart'
* executing "cd /home/foo/bar/current && rake RAILS_ENV=beta queue:restart_workers"
servers: ["208.0..."]
[208.0...] executing command
*** [err :: 208.0...] rake aborted!
*** [err :: 208.0...] no such file to load -- bundler/setup
*** [err :: 208.0...] /home/foo/bar/releases/20120630161947/Rakefile:5
*** [err :: 208.0...] (See full trace by running task with --trace)
** [out :: 208.0...] (in /home/foo/bar/releases/20120630161947)
command finished in 1183ms
failed: "sh -c 'cd /home/foo/bar/current && rake RAILS_ENV=beta queue:restart_workers'" on 208.0...
The relevant part on deploy.rb looks like this:
task :restart do
run "cd /home/foo/bar/current && rake RAILS_ENV=#{CAP_ENV} queue:restart_workers"
run "cd /home/foo/bar/current && rake RAILS_ENV=#{CAP_ENV} db:migrate"
run "touch #{deploy_to}/current/tmp/restart.txt"
end
Needless to say, rake RAILS_ENV=beta queue:restart_workers' works perfectly when run manually on the server. Also, the application gets deployed (the code was copied from git, it's just the last part of the deploy fails).
Lastly, the error didn't change since before I installed rvm-capistrano and added to the Gemfile, so I'm not even sure it's related to rvm, I'm just guessing from looking online.
Thanks
In order to work correctly, rbenv must override all the ruby and gem-related executables with the shims it provides.
Usually this is done with a startup script (this is why it works when you login to your server) but Capistrano logs in without shell and thus does not run those scripts.
You must add the following to your deploy.rb :
set :default_environment, {
'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
}
and do not use anything rvm related on your server, rbenv and rvm really do not like each other
PS : a little more explanation on this topic : http://henriksjokvist.net/archive/2012/2/deploying-with-rbenv-and-capistrano/

Resources