How to check rails environment? - ruby-on-rails

How check rails environment on Ubuntu Server?
command: Rails.env => command not found
command: rails.env => command not found

One liner if you are in app root
rails r "puts Rails.env"

It sounds like you tried to run Rails.env in a shell. That won't work because Rails.env is Ruby code, not a Unix shell command.
How are you deploying and starting your rails app on the server? The Rails environment is determined by whatever the value of the RAILS_ENV environment variable is when the server starts. You might have some configuration file somewhere that specifies it, or maybe you just start your server with a command of the form RAILS_ENV=production my_rails_server? I would need to know more details about exactly what commands you run to start the server in order to really answer this. Are you using unicorn, mongrel, Webrick, or something else?

You can check complete details about your rails app. By typing this command "rake about". Will give you brief details about which version of ruby have you installed on your machine, rails version etc. For example -
About your application's environment
Rails version ------> 4.2.6
Ruby version ------> 2.3.1-p112 (x86_64-linux)
RubyGems version ----> 2.5.1
Rack version ----> 1.6.4
JavaScript Runtime -------> Node.js (V8)
Middleware ------> Rack::Sendfile, ActionDispatch::Static,
Application root ----> /data/www/testapp
Environment ------> development
Database adapter -----> mysql2
Database schema version -----> 0

On your Rails Application directory type :
rake about

rails r -e production 'p Rails.env'
production
rails r -e production 'p Rails.env.production?'
true
rails r 'p Rails.env'
development
rails r -e development 'p Rails.env.development?'
true
rails r -e test 'p Rails.env.test?'
true
PS If rails command not found try to use path bin/:
bin/rails r 'p Rails.env'
development
PS2 If use rvm, check installed ruby versions:
rvm list
ruby-2.2.0 [ x86_64 ]
ruby-2.2.4 [ x86_64 ]
ruby-2.6.2 [ x86_64 ]
ruby-2.7.0 [ x86_64 ]
ruby-2.7.1 [ x86_64 ]
=> ruby-2.7.2 [ x86_64 ]
* ruby-2.7.3 [ x86_64 ]
ruby-3.0.0 [ x86_64 ]
# => - current
# =* - current && default
# * - default
Select to version:
rvm use ruby-3.0.0
Bundle install:
bundle

You can also check your environment from your Rails console in the shell. Start at the application directory path.
rails console<enter>
after you see the output from your console... (your output will most likely differ)
Running via Spring preloader in process XXXXX
Loading development environment (Rails X.x.x)
irb(main):001:0>
At the promt type
Rails.env<enter>
Unless you have custom environments, one of the following environment is loaded
=> "development"
=> "production"
=> "test"

Related

Getting rails console to work with multiple Gemfiles

We are currently migrating from Rails 4 to 5, and have two Gemfiles (similar to how GitHub did it), Gemfile (Rails 4) and Gemfile_5 (Rails 5).
The following commands work as expected:
bundle exec rails s
=> Booting WEBrick
=> Rails 4.2.11.12 LTS application starting in development on http://localhost:3000
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails s
=> Booting WEBrick
=> Rails 5.0.7.1 application starting in development on http://localhost:8000
bundle exec rspec spec/...
# runs specs using Rails 4 gemset
BUNDLE_GEMFILE=Gemfile_5 bundle exec rspec spec/...
# runs specs using Rails 5 gemset
bundle exec rails --version
Rails 4.2.11
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails --version
Rails 5.0.7.1
And yet, when trying to run console or runner, it'll only use the Rails 4 gemset:
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails c
Loading development environment (Rails 4.2.11.12 LTS)
BUNDLE_GEMFILE=Gemfile_5 bundle exec rails r "puts Rails.version"
4.2.11
I've tried restarting Spring, but that hasn't had any effect. What am I missing here?
The trick was to fully disable Spring, rather than just restart it:
DISABLE_SPRING=1 BUNDLE_GEMFILE=Gemfile_5 bundle exec rails console
Loading development environment (Rails 5.0.7.1)

Cannot load Rails due to Switch to inspect mode error

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.

Rails console default environment

On my development machine:
$ bundle exec rails console
Loading development environment (Rails 3.2.3)
1.9.3p194 :001 > Rails.env
=> "development"
This is expected. So far, so good.
Yet on my production server (to which I have deployed using Capistrano), I get exactly the same result:
$ bundle exec rails console
Loading development environment (Rails 3.2.3)
1.9.3p194 :001 > Rails.env
=> "development"
On either machine, I can instead do:
$ bundle exec rails console production
Loading development environment (Rails 3.2.3)
1.9.3p194 :001 > Rails.env
=> "production"
My question is: on the production server, shouldn't bundle exec rails console load the production environment by default, instead of the development environment? And if not, why not?
The rails executable can't know which environment should run on which machine.
you can put export RAILS_ENV=production in your ~/.bashrc or ~/.bash_profile file of the user you want to start the console with.
RAILS_ENV is a variable like any other which will always default to development
if you like you can always open up '~/.bash_profile' on the production server and add this:
alias sc="bundle exec rails console production"
then run source ~/.bash_profile to reload that file for your terminal session and you can just call sc to load up console.

How to start rails server?

I am developing rails 2.3.2 application.
When I type the command "rails script/server"
I got the following output instead of server starting why?
rails script/server
Usage:
rails new APP_PATH [options]
Options:
-J, [--skip-javascript] # Skip JavaScript files
[--dev] # Setup the application with Gemfile pointing to your Rails checkout
[--edge] # Setup the application with Gemfile pointing to Rails repository
-G, [--skip-git] # Skip Git ignores and keeps
-m, [--template=TEMPLATE] # Path to an application template (can be a filesystem path or URL)
-b, [--builder=BUILDER] # Path to a application builder (can be a filesystem path or URL)
[--old-style-hash] # Force using old style hash (:foo => 'bar') on Ruby >= 1.9
[--skip-gemfile] # Don't create a Gemfile
-d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
# Default: sqlite3
-O, [--skip-active-record] # Skip Active Record files
[--skip-bundle] # Don't run bundle install
-T, [--skip-test-unit] # Skip Test::Unit files
-S, [--skip-sprockets] # Skip Sprockets files
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /home/xichen/.rvm/rubies/ruby-1.8.7-p352/bin/ruby
-j, [--javascript=JAVASCRIPT] # Preconfigure for selected JavaScript library
# Default: jquery
Runtime options:
-q, [--quiet] # Supress status output
-s, [--skip] # Skip files that already exist
-f, [--force] # Overwrite files that already exist
-p, [--pretend] # Run but do not make any changes
Rails options:
-h, [--help] # Show this help message and quit
-v, [--version] # Show Rails version number and quit
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
Example:
rails new ~/Code/Ruby/weblog
This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
See the README in the newly created application to get going.
When I type linux command "ls" I got the following directories and files showing:
app Capfile config criptq db doc features Gemfile Gemfile.lock generate lib log nbproject public Rakefile README script spec test tmp vendor
my Gemfile is:
source "http://rubygems.org"
gem "rails", "2.3.2"
gem "mysql", "2.8.1"
gem "fastercsv"
gem "will_paginate", "2.3.16"
gem "chronic", "0.6.4"
gem "whenever", "0.4.1"
gem "searchlogic", "2.4.28"
group :development do
gem "mongrel", "1.1.5"
end
group :test do
gem "rspec", "1.3.2"
gem "rspec-rails", "1.3.4"
gem "factory_girl", "1.3.3"
end
In a Rails 2.3 app it is just ./script/server start
For rails 3.2.3 and latest version of rails you can start server by:
First install all gem with command: bundle install or bundle.
Then Configure your database to the database.yml.
Create new database: rake db:create
Then start rails server.
rails server orrails s
For rails 2.3.2 you can start server by:
ruby script/server
In rails 2.3.x application you can start your server by following command:
ruby script/server
In rails 3.x, you need to go for:
rails s
Make sure you're in the right directory when you start the server
sites>yoursite> rails s
On rails 3, the simpliest way is rails s.
In rails 2, you can use ./script/server start.
You can also use another servers, like thin or unicorn, that also provide more performance.
I use unicorn, you can easily start it with unicorn_rails.
BTW, if you use another things, like a worker (sidekiq, resque, etc), I strongly recommend you to use foreman, so you can start all your jobs in one terminal windows with one command and get a unified log.
For rails 4.1.4 you can start server:
$ bin/rails server
Goto root directory of your rails project
In rails 2.x run > ruby script/server
In rails 3.x use > rails s
For newest Rails versions
If you have trouble with rails s, sometimes terminal fails.
And you should try to use:
./bin/rails
To access command.
For the latest version of Rails (Rails 5.1.4 released September 7, 2017), you need to start Rails server like below:
hello_world_rails_project$ ./bin/rails server
=> Booting Puma
=> Rails 5.1.4 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.10.0 (ruby 2.4.2-p198), codename: Russell's Teapot
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
More help information:
hello_world_rails_project$ ./bin/rails --help
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")
test Run tests except system tests (short-cut alias: "t")
test:system Run system tests
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"
If you are in rails2 version then to start the server you have do,
script/server or
./script/server
But if you are in rails3 or above version then to start the server you have do,
rails server or
rails s
in rails 2.3.X,just type following command to start rails server on linux
script/server
and for more help read "README" file which is already created in rails project folder
I also faced the same issue, but my fault was that I was running "rails s" outside of my application directory.
After opening the cmd, just go inside your application and run the commands from their, it worked for me.
You have to cd to your master directory and then rails s command will work without problems.
But do not forget bundle-install command when you didn't do it before.
run with nohup to run process in the background permanently if ssh shell is closed/logged out
nohup ./script/server start > afile.out 2> afile.err < /dev/null &
Rails version < 2
From project root run:
./script/server
I believe this is what happens if "rails new [project]" has not actually executed correctly. If you are doing this on windows and "rails server" just returns the help screen, you may need to restart your command prompt window and likely repeat your setup instructions. This is more likely true if this is your first time setting up the environment.

Rails 3.1 Deployment to Heroku Error

I'm trying to deploy my app to Heroku, I've done this before on my Windows machine, and now I am currently using a mac.
I'm trying to use Postgresql for the first time.
I have the following in my Gemfile:
gem 'pg'
EDIT:
AndrewDavis-OSX:lunchbox ardavis$ rvm list
rvm rubies
=> ruby-1.9.2-p180 [ x86_64 ]
AndrewDavis-OSX:lunchbox ardavis$ heroku rake db:migrate
rake aborted!
/app/config/initializers/session_store.rb:3: syntax error, unexpected ':', expecting $end
App::Application.config.session_store :cookie_store, key: '_app_session'
^
(See full trace by running task with --trace)
(in /app)
As you can see, I am running ruby 1.9.2. And there is the error for my heroku migration.
EDIT 2:
Just created a brand new rails app using Rails 3.1.rc1. I set the gemfile to include
group :production do
gem 'therubyracer-heroku', '0.8.1.pre3'
gem 'pg'
end
I did a quick git init, commited, then 'heroku create' and 'git push heroku master'. Those all work just fine. However the problem is when I try 'heroku rake db:migrate'. I get the same error that you see above.
TEMP FIX EDIT:
So... if I change my config/initializers/session_store.rb from
App::Application.config.session_store :cookie_store, key: '_app_session'
to
App::Application.config.session_store :cookie_store, :key => '_app_session'
and change my config/initializers/wrap_parameters.rb from
ActionController::Base.wrap_parameters format: [:json]
to
ActionController::Base.wrap_parameters :format => [:json]
Then I'm able to do 'heroku rake db:migrate' just fine. Anyone care to explain why this works locally the original way, without any modification of the colons/hashes? The original way is the generated default from doing 'rails new myApp'
The Heroku stack needs to be migrated, you can run this command to do so:
heroku stack:migrate bamboo-mri-1.9.2
I was running 1.9.2 locally, which is why it was working locally. But on Heroku, it was running 1.8.7.
The problem is that there is a new-style hash argument available in Ruby 1.9.2 but unavailable in Ruby 1.8.7 which is:
key: value # only available in 1.9.2 but
:key => value # available in 1.8.7 and 1.9.2
This is just an additional pointer to some. If ever you are getting the same error in your development environment, on an app that was functioning just fine moments ago, check your ruby version as Preksha/Alex Kliuchnikau mentioned above.
$ ruby -v
If ruby is not set to 1.9.2 or above, you can do that with rvm
$ rvm --default 1.9.2 (1.9.3 is what I currently use)
If it's not responding to rvm command, add rvm to your bashrc file by copying the following line in your terminal:
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" ' >> ~/.bash_profile

Resources