rails console in not accepting the commands - ruby-on-rails

Rails console is loading but not accepting any commands .I am unable to even close the console by typing the exit command.
Loading development environment (Rails 7.0.2.4)
irb(main):001:0> ^C
irb(main):001:0> ^C
irb(main):001:0>
I have ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x64-mingw-ucrt] version

Typing exit and pressing Enter will exit the console.
irb(main):001:0> exit
IRB pocket reference here which may help: https://www.oreilly.com/library/view/ruby-pocket-reference/9780596514815/ch01s27.html

Looks like you're typing ctrl+C, which will cancel a process that is currently running. To exit the console type ctrl+D

There could be some environment issues as well, like if the environment is not correctly set up then the console may not work as expected or you can also try to specify the environment using the command rails console production or rails console development
& also try to check the database.yml there could be some issue there as well

Related

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.

Revert rails console changes

I have an application where I run rake tasks to add data from csv files. I need to test it from the console first. Is there a command in console to revert all the changes as its meant only for testing.
You need to run the console in sandbox mode.
rails c --sandbox
you can run:
bundle exec rails c -s
with -s option all commands are executed in sandbox mode. After exiting the console all commands will be rolled back. (you can use --sandbox or the shorter form -s)
you can start the console in sandbox mode. It will wrap the whole session in a db transaction which will be rollbacked when you close the console.
To start it:
rails console --sandbox

Running Rails console on heroku, in sandbox mode

Couldn't find it in the docs, nor on SO, but is there a way to run the Rails (3.2.x) console in sandbox mode on heroku (Celadon Cedar), equivalent to
rails console --sandbox
For a more "the Heroku way" alternative, heroku run console --sandbox does the trick as well:
$ heroku run console --sandbox
Running `console --sandbox` attached to terminal... up, run.6024
[...]
Loading production environment in sandbox (Rails 3.2.12)
Any modifications you make will be rolled back on exit
irb(main):001:0>

sending a command to heroku console

What's the correct way of sending a command to heroku console on Cedar?
heroku console 2+2 works on older stacks - it warns me to use heroku run console on cedar.
When I run
heroku run console 2+2
it loads the unwanted "2+2" environment and opens up a console.
Loading 2+2 environment (Rails 3.2.3)
irb(main):001:0>
So what's the proper way of sending a command to heroku console on Cedar?
It's not the prettiest solution, but piping the command in did work.
echo "2 + 2" | heroku run console
Looking at the code, it appears there is no (current) built in way to do it.

Rails 3 Sandbox Console

In Rails 2 you're able to run
script/console --sandbox
so you can play with production data and not accidentally break anything.
I can't seem to find the equivalent command for Rails 3. Does anyone know what it is?
Easy, type in:
bundle exec rails c -s
and that is it.
$ bundle exec rails c --help
Usage: console [environment] [options]
-s, --sandbox Rollback database modifications on exit.
--debugger Enable ruby-debugging for the console.
--irb DEPRECATED: Invoke `/your/choice/of/ruby script/rails console` instead
It is simple, but, sometimes, if you are not running rails executable using bundle exec, it may, or may not, result in an error. In order to avoid this, ALWAYS use bundle exec.
To quote bundler page (if not documentation):
In some cases, running executables without bundle exec may work, if
the executable happens to be installed in your system and does not
pull in any gems that conflict with your bundle.
However, this is unreliable and is the source of considerable pain.
Even if it looks like it works, it may not work in the future or on
another machine.

Resources