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.
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).
So starting from Rails 4.1.x there seems to be a recommended way to use rails under the application folder. Instead of the traditional:
rails server
it is recommended by Rails official guide to use
bin/rails server
It looks like the bin/rails is referencing rails with additional stuff. What would be the additional benefits of using bin/rails compared to rails?
A second question is - I was used to use rails server, rails console, etc. rather than bin/rails server, bin/rails console. Without using bin/rails, would I lose anything (like misloading some libs, etc.)?
Thanks.
Put the following line in your bin/rails file: puts "In the bin/rails file"
Now run rails server. You'll likely see that the rails command is executing the bin/rails file.
I'm guessing the official guide suggests using bin/rails for two reasons:
Avoid using another instance of rails if your paths are not set up properly.
Speed - bin/rails seems to be a bit faster than just rails
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 :)
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.
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