Using Rails 3. In my project directory am trying to launch rails script/console by using "rails console" command line & get this in return.
Loading test environment (Rails 3.2.1)
irb(main):001:0>
I think you may be confused about the rails commands.
rails server (or script/server when using Rails 2.x) is used to start a web server for local development (this by default is Webrick running on 0.0.0.0:3000). This process runs in the foreground and does not allow for interaction. It will log output to STDOUT.
rails console (or script/console when using Rails 2.x) is used to start the interactive ruby shell (irb) with your Rails app and environment (development by default, test in your case) loaded. This is an interactive shell meaning that you can type ruby code in here and it will be executed when you hit the return key or when it encounters the end of a block. Try this out
a = ["b", "a", "r","t"]
a.reverse
Will return
=> ["t", "r", "a", "b"]
Since this also loads your Rails application, you have access to the classes defined in your application. For example, if you have a Person model defined, you can instantiate a new instance by typing the following into irb
Person.new
To leave the irb, you can type exit to return to your operating system's shell. I hope this helps to clear up some of the confusion.
Related
I am new on rails and I am working on a practice project. I was trying to get the rails console started. However, when I run rails c I get the following:
"Running via Spring preloader in process 4495
Loading development environment (Rails 5.2.3)
Cannot read termcap database;
using dumb terminal settings.
[1] pry(main)> "
I would really like to understand what is happening and a solution would be more that appreciated!
If you have the 'pry' debugger enabled (e.g. pry-byebug in your Gemfile) you'll get the [1] pry(main)> prompt. It isn't an error. You can set breakpoints and so on as documented. If you don't want it, remove the gem.
The Cannot read termcap database; using dumb terminal settings. is separate-ish. It means the app wants to use features of your terminal emulator, and it can't, because it can't see a file that tells it about that.
This will be something to do with your OS config (Windows, OSX, Ubuntu, etc).
I'm getting some annoying errors when trying to make system calls in the rails console in a production environment.
Loading production environment (Rails 4.0.2)
2.0.0 :001 > `hostname`
bin/rails: No such file or directory - hostname
=> nil
It works fine in irb.
$ irb
2.0.0-p353 :001 > `hostname`
=> "app-1\n"
Same thing happens on my local machine. rails c is fine but rails c production gives the error above.
ruby -v outputs ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]
rails --version outputs Rails 4.0.2
I'm not really sure where to start debugging this so any advice is welcome! I have tried running rake rails:update:bin, based on "bin/rails: No such file or directory" w/ Ruby 2 & Rails 4 on Heroku, but it didn't seem to make a difference. Any ideas?
The difference in the PATH is the problem. You'll need to look into your code or hosting environment to find out how the PATH is being set in the rails app. It could be in the app itself, or whatever app server is launching the app - details can vary wildly.
Your rails console output:
"$HOME/.rvm/gems/ruby-1.9.3-p429/bin:$HOME/.rvm/bin:$PATH"
indicates that something is trying to add to the path, but it is inserting literal dollar signs instead of expanding the existing environment variable. For example in a shell script setting a path, if you used the wrong quote symbols you'd see something like what you've got.
export PATH="$HOME/.rvm/gems/ruby-1.9.3-p429/bin:$HOME/.rvm/bin:$PATH" # right
export PATH='$HOME/.rvm/gems/ruby-1.9.3-p429/bin:$HOME/.rvm/bin:$PATH' # wrong
I hope this points you in the right direction :)
total n00b question here but I can't seem to find the answer searching around here or the web.
I have Ruby v1.8.7 (2009-06-12 patchlevel 174) and Rails v.2.3.5 installed on my Mac (10.6.8). I have no problem generating a Rails directory and starting the Ruby server, but what happens is that once the server is started I have no prompt and no code I type seems to execute.
to be specific - here is the terminal code from a session:
bvb:new bennettvonbennett$ ruby script/server
=> Booting Mongrel
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
after this point there is no prompt and no command I type seems to produce a result.
what am I doing wrong?
This is the correct behavior. You're not doing anything wrong.
The server runs in the foreground so that you can see the log output on every page load without having to open a specific log file. It's very helpful for debugging and staying aware of what the code you write does to the application and the database.
To return to a normal command prompt, you can send an end-of-text character (usually ctrl-c) to stop the server. Most of us Rails-folk work with multiple terminal windows open - server running in one, and others for other functions, such as the rails console, started with
ruby script/console
which will let you experiment with other Rails controller functions, much like irb for standard Ruby.
With ruby script/server you launch the webserver. Run ruby script/console if you want to execute commands against your current Rails application.
You're loooking for:
script/console
If you want to play with your project in a console.
I'm running Rails 3.1.1 and Ruby 1.8.7. When I type irb in the OS X Terminal the command prompt ree-1.8.7-2011.03 :001 > is displayed immediately. When I type rails console in the terminal the same command prompt appears after a 1-2 second delay. Also the terminal displays Loading development environment (Rails 3.1.1).
What exactly does the Rails development environment entail?
When you do rails console it's loading the entire Rails stack (controllers, models, helpers, etc. and all the supporting code for those) as well as connecting to your Rails database. When you just do irb, all you're loading is the Ruby interpreter.
The rails console is a ruby Console with all the fancy stuff from rails and the app from where you are calling the console.
In the rails console you can directly create or find objects from your current app. It will use active record and the database connection. It is a great way to quickly check syntax and logic as you code.
My client wants me to download a working ruby on rails project to my local system. But when I connected to a server, I got nine identical project folders (which seem like different versions). Is there a way to know which project is running using process id (I have only the process id)?
If you have the process ID, then check /proc/[pid]/cwd symbolic link. This is the process' current directory.
I assume that you are on a Linux-like system.
If you are not, then you may issue a HTTP request (to wake up the rails app) and then look which logfile has been modified recently.
if you are able to run rails console on your server then type this: Rails.root in your console or irb it will tell that project you are on belongs to which directory!
no we cant know using process id. but when you start server you will find rails version like
:> ruby script/server
=> Booting Mongrel
=> Rails 2.3.10 application starting on http://0.0.0.0:3000
above line using rails 2.3.10 version and if you want to know ruby version simply call
ruby -v in your command prompt