I an trying to open my root page it shows error "Internal server error"
=> Booting Thin
=> Rails 4.2.6 application starting in development on http://localhost:4000
=> Run rails server -h for more startup options
=> Ctrl-C to shutdown server
Thin web server (v1.6.4 codename Gob Bluth)
Maximum connections set to 1024
Listening on localhost:4000, CTRL+C to stop
DEPRECATION WARNING: You didn't set secret_key_base. Read the upgrade documentation to learn more about this new config option. (called from require at /home/smk/test/test/BMS-New/bin/rails:9)
Unexpected error while processing request: Missing secret_token and secret_key_base for 'development' environment, set these values in config/secrets.yml
/home/smk/test/test/BMS-New/vendor/bundle/gems/railties-4.2.6/lib/rails/application.rb:534:in validate_secret_key_config!'
/home/smk/test/test/BMS-New/vendor/bundle/gems/railties-4.2.6/lib/rails/application.rb:246:inenv_config'
/home/smk/test/test/BMS-New/vendor/bundle/gems/railties-4.2.6/lib/rails/engine.rb:514:in call'
/home/smk/test/test/BMS-New/vendor/bundle/gems/railties-4.2.6/lib/rails/application.rb:165:incall'
/home/smk/test/test/BMS-New/vendor/bundle/gems/rack-1.6.4/lib/rack/content_length.rb:15:in call'
/home/smk/test/test/BMS-New/vendor/bundle/gems/thin-1.6.4/lib/thin/connection.rb:86:inblock in pre_process'
/home/smk/test/test/BMS-New/vendor/bundle/gems/thin-1.6.4/lib/thin/connection.rb:84:in catch'
/home/smk/test/test/BMS-New/vendor/bundle/gems/thin-1.6.4/lib/thin/connection.rb:84:inpre_process'
/home/smk/test/test/BMS-New/vendor/bundle/gems/thin-1.6.4/lib/thin/connection.rb:53:in process'
/home/smk/test/test/BMS-New/vendor/bundle/gems/thin-1.6.4/lib/thin/connection.rb:39:inreceive_data'
/home/smk/test/test/BMS-New/vendor/bundle/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:194:in run_machine'
/home/smk/test/test/BMS-New/vendor/bundle/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:194:inrun'
/home/smk/test/test/BMS-New/vendor/bundle/gems/thin-1.6.4/lib/thin/backends/base.rb:73:in start'
/home/smk/test/test/BMS-New/vendor/bundle/gems/thin-1.6.4/lib/thin/server.rb:162:instart'
/home/smk/test/test/BMS-New/vendor/bundle/gems/rack-1.6.4/lib/rack/handler/thin.rb:19:in run'
/home/smk/test/test/BMS-New/vendor/bundle/gems/rack-1.6.4/lib/rack/server.rb:286:instart'
/home/smk/test/test/BMS-New/vendor/bundle/gems/railties-4.2.6/lib/rails/commands/server.rb:80:in start'
/home/smk/test/test/BMS-New/vendor/bundle/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:80:inblock in server'
/home/smk/test/test/BMS-New/vendor/bundle/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in tap'
/home/smk/test/test/BMS-New/vendor/bundle/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:inserver'
/home/smk/test/test/BMS-New/vendor/bundle/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in run_command!'
/home/smk/test/test/BMS-New/vendor/bundle/gems/railties-4.2.6/lib/rails/commands.rb:17:in'
/home/smk/test/test/BMS-New/bin/rails:9:in require'
/home/smk/test/test/BMS-New/bin/rails:9:in'
/home/smk/test/test/BMS-New/vendor/bundle/gems/spring-1.7.1/lib/spring/client/rails.rb:28:in load'
/home/smk/test/test/BMS-New/vendor/bundle/gems/spring-1.7.1/lib/spring/client/rails.rb:28:incall'
/home/smk/test/test/BMS-New/vendor/bundle/gems/spring-1.7.1/lib/spring/client/command.rb:7:in call'
/home/smk/test/test/BMS-New/vendor/bundle/gems/spring-1.7.1/lib/spring/client.rb:30:inrun'
/home/smk/test/test/BMS-New/vendor/bundle/gems/spring-1.7.1/bin/spring:49:in <top (required)>'
/home/smk/test/test/BMS-New/vendor/bundle/gems/spring-1.7.1/lib/spring/binstub.rb:11:inload'
/home/smk/test/test/BMS-New/vendor/bundle/gems/spring-1.7.1/lib/spring/binstub.rb:11:in <top (required)>'
/usr/local/lib/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:inrequire'
/usr/local/lib/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require'
/home/smk/test/test/BMS-New/bin/spring:13:in'
bin/rails:3:in load'
bin/rails:3:in'
In your config/secrets.yml file remove everything add this:
development:
secret_key_base: bcbfd4e5010b035e62898f964188362877783b43f94bf883cf4b1d82d9224349789fb9dd0c2f33a200acfa266024a285e324caa3b20d0a9a352014156e47ec5c
test:
secret_key_base: af068d8662bf954a2afeb27c64bbcdeb56b730029272bd3daff7e00c3c076999b3d2297747a3cef0550d3efd5cf8072e0477b8a62f096c94ab124f0d99320d20
# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
Your error refers that in your secrets.yml file secret_key_base and secret_token is missing.
FYI: You can change the secret_key_base as you like. If you don't have this file then create one. For different environment the secret_key_base will be different. You should add the environment there also. Here, I give key base for development/production/test environment
Related
I'm trying to learn Rails from the Rails Tutorial book, got lubuntu so I could try to get the best experience possible. Now I'm stuck on something that should be easy and straight forward.
We're creating a little "toy app" a very basic twitter like thing, still at the very beginning, we used:
$ rails generate scaffold User name:string email:string
To generate a User Table if I'm not mistaken and then:
$ rails db:migrate
I guess to update a preexisting table structure (not sure, but it's said that later everything will be explained).
Finally we are supposed to run this:
$ rails server -b $IP -p $PORT
In a differente tab, I did and got this error:
~/workspace/toy_app$ rails server -b $IP -p $PORT
=> Booting Puma
=> Rails 5.0.0.1 application starting in development on http://-p:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.4.0 (ruby 2.3.1-p112), codename: Owl Bowl Brawl
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://-p:3000
Exiting
/var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:255:in `initialize': getaddrinfo: Name or service not known (SocketError)
from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:255:in `new'
from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:255:in `add_tcp_listener'
from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:102:in `block in parse'
from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:85:in `each'
from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/binder.rb:85:in `parse'
from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/runner.rb:129:in `load_and_bind'
from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/single.rb:84:in `run'
from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/puma/launcher.rb:172:in `run'
from /var/lib/gems/2.3.0/gems/puma-3.4.0/lib/rack/handler/puma.rb:51:in `run'
from /var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/server.rb:296:in `start'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/server.rb:79:in `start'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:90:in `block in server'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:85:in `tap'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:85:in `server'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /var/lib/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from /home/zero/workspace/toy_app/bin/rails:9:in `require'
from /home/zero/workspace/toy_app/bin/rails:9:in `<top (required)>'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `load'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `call'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/client/command.rb:7:in `call'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/client.rb:30:in `run'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/bin/spring:49:in `<top (required)>'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `load'
from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `<top (required)>'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/zero/workspace/toy_app/bin/spring:13:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'
Is anything corrupted? Did I do anything wrong? I went to check the files I remembered seeing puma (in the app folder) on and it's in the gem file:
gem 'puma', '3.4.0'
And the puma.rb file (took out the comments):
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count
port ENV.fetch("PORT") { 3000 }
environment ENV.fetch("RAILS_ENV") { "development" }
plugin :tmp_restart
I was going to put all the files mentioned but it's tons of code, if there's any that's useful I'll post it, I don't want to spam with possible useless code.
$IP is not defined in your environment, so rails server is trying to use -p as the IP address. Try this:
rails server -b ${IP:-127.0.0.1} -p ${PORT:-3000}
Alternatively, set these environment variables with export before running rails server -b $IP -p $PORT:
export IP=127.0.0.1
export PORT=3000
Or if you're fine with Rails' defaults, just forget all this and run rails server. :-)
If you are running locally you should be able to use rails server without arguments. If you are using the Hartl tutorial, it presumes you are using a web-based IDE that requires the extra arguments.
If you are using cloud based IDE, you need to listen at particular IP & PORT. For example, C9, you need to listen to 0.0.0.0 and 8080. So they have setup environment variables, $IP & $PORT.
If you execute,
echo $IP ###gives 0.0.0.0
echo $PORT ###gives 8080
rails s -b $IP -p $PORT ##Rails x.x.x application starting in development on http://0.0.0.0:8080
The -b option binds Rails to the specified IP, by default it is localhost.
If you are on your local machine, run
rails s ### Rails x.x.x application starting in development on http://localhost:3000
I was going through puma upstart script and I tried to set it up on my server. But, after some trial and error I realized that I shouldn't be doing this manually and my deployment script i.e. capistrano should be doing this for me.
I google this but I could not much other than 2 lines on capistrano-puma:
For Jungle tasks (beta), these options exist:
set :puma_jungle_conf, '/etc/puma.conf'
set :puma_run_path, '/usr/local/bin/run-puma'
I am not sure how to use this and make capistrano do this rather than me manually doing it.
Work done so far:
I followed this blog- Deploying a Rails App on Ubuntu 14.04 with Capistrano, Nginx, and Puma and it works like a charm, except that I have to remember to run cap production puma:restart if I restart the server (or the puma process dies and I get cloudwatch alarms).
Following the official docs, I made a config file /etc/puma.conf:
/home/deploy/apps/<app_name>/current
but it is unable to find puma.rb- Naturally, as puma.rb is in
<app_name>/shared/puma.rb
and not in
<app_name>/current/config/puma.rb
The actual error I get in the logs:
/home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/dsl.rb:26:in `read': No such file or directory # rb_sysopen - config/puma.rb (Errno::ENOENT)
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/dsl.rb:26:in `_load_from'
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/dsl.rb:9:in `load'
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/configuration.rb:204:in `block in load'
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/configuration.rb:201:in `each'
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/configuration.rb:201:in `load'
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/launcher.rb:62:in `initialize'
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/cli.rb:65:in `new'
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/cli.rb:65:in `initialize'
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/gems/puma-3.4.0/bin/puma:8:in `new'
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/gems/puma-3.4.0/bin/puma:8:in `<top (required)>'
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/bin/puma:23:in `load'
from /home/deploy/apps/voggle/shared/bundle/ruby/2.2.0/bin/puma:23:in `<main>'
I can debug an app locally with binding.pry just fine. But I need to that on the remote server (webiste) as well, because locally I don't have any error but on production I do. So put binding.pry in the source file on the server and when I made a post request to the website, I didn't return any response and even when I ran rails console on the server didn't change anything and there was no output in rails console.
It's a dev server, so it's visible in the internet but it's a developer server, although for rails it's production because RAIL_ENV is equal to production. I really need to debug it the way I can do locally using pry. How can I do it?
update:
# bundle exec pry-remote
/usr/lib/ruby/1.9.1/drb/drb.rb:736:in `rescue in block in open': druby://127.0.0.1:9876 - #<Errno::ECONNREFUSED: Connection refused - connect(2)> (DRb::DRbConnError)
from /usr/lib/ruby/1.9.1/drb/drb.rb:730:in `block in open'
from /usr/lib/ruby/1.9.1/drb/drb.rb:729:in `each'
from /usr/lib/ruby/1.9.1/drb/drb.rb:729:in `open'
from /usr/lib/ruby/1.9.1/drb/drb.rb:1191:in `initialize'
from /usr/lib/ruby/1.9.1/drb/drb.rb:1171:in `new'
from /usr/lib/ruby/1.9.1/drb/drb.rb:1171:in `open'
from /usr/lib/ruby/1.9.1/drb/drb.rb:1087:in `block in method_missing'
from /usr/lib/ruby/1.9.1/drb/drb.rb:1105:in `with_friend'
from /usr/lib/ruby/1.9.1/drb/drb.rb:1086:in `method_missing'
from /web/my_site.com/releases/201501271120/vendor/bundle/ruby/1.9.1/gems/pry-remote-0.1.8/lib/pry-remote.rb:289:in `run'
from /web/my_site.com/releases/201501271120/vendor/bundle/ruby/1.9.1/gems/pry-remote-0.1.8/bin/pry-remote:4:in `<top (required)>'
from /web/my_site.com/releases/201501271120/vendor/bundle/ruby/1.9.1/bin/pry-remote:19:in `load'
from /web/my_site.com/releases/201501271120/vendor/bundle/ruby/1.9.1/bin/pry-remote:19:in `<main>'
I assume your production app is run by some kind of daemonized application server, such as Puma or Unicorn.
In this setup, pry-remote can help.
add it to your Gemfile
use binding.remote_pry instead of binding.pry
execute bundle exec pry-remote on your server
You'll get a pry shell as soon as the breakpoint has been hit.
In my Rails app I use whenever gem to run a Sideqik task. When I run that command manually in the rails console it works. But the Whenever cron fails with the following error : It keeps repeating in the log. How can I fix this? What is the problem?
/home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection': 'production' database is not configured. Available: ["development", "test"] (ActiveRecord::AdapterNotSpecified)
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:211:in `resolve_connection'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:139:in `resolve'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:169:in `spec'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:50:in `establish_connection'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:27:in `each'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/railtie.rb:116:in `block in <class:Railtie>'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `instance_exec'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `run'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each'
from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `call'
from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:54:in `run_initializers'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/application.rb:352:in `initialize!'
from /home/pubudu/Projects/istockseller/config/environment.rb:5:in `<top (required)>'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:92:in `preload'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:143:in `serve'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:131:in `block in run'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:125:in `loop'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:125:in `run'
from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application/boot.rb:18:in `<top (required)>'
from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
You're encountering this error because your production settings aren't configured correctly to connect to your database. When you run the command from the Rails console, you're connecting to the development environment, but your cron job is being run in the production environment.
Rails has 3 built-in environments. The main difference is that each has their own database, but there are other differences too.
Production, for when your code is running live. This environment does a lot of caching, and doesn't show development error messages to the user.
Development, for building your application. This is the default environment, and is the environment that you spend most of your time in.
Test, for running automated tests. The main reason this is separate is so your test suite can empty & recreate the database on each test run. This makes your tests more reproducible, and stops an accidental test run from destroying your development DB.
Mostly, these are configured via files in config/environments. The database connections are configured in config/database.yml - you'll find YAML keys for development, production, and test in there. The Rails Guide has more details about configuring applications.
You can find out which environment you're running in via Rails.env. For instance:
user#foo $ rails c
Loading development environment (Rails 4.1.1)
irb(main):001:0> Rails.env
"development"
And you can force a command to run in a given environment via a RAILS_ENV shell environment variable:
user#foo $ RAILS_ENV=production rails c
Loading production environment (Rails 4.1.1)
irb(main):001:0> Rails.env
"production"
To fix your problem, you have two choices:
Force your cron command to run in the development environment. It sounds like your app is running in the development environment (because it works from the console, and you have no production database configured). So forcing the cron job to run in the development environment should fix this. You could set it in your cronjob directly by setting the RAILS_ENV variable as we saw above, but whenever lets you specify the environment on the commandline or within your schedule.rb:
set :environment, "development"
Fix your app to run in production. If your application is live, it really should be running in the production environment. It will be faster and more secure that way - as well as letting you mess things up when developing new features without the risk of destroying something used by your users. You'll have to set up the database, and will have to test your app in production. The best way to do this varies by platform; this Stack Overflow question has a good summary of the various options.
I have repeatedly tried to setup my rails app on an ec2 instance using nginx and unicorn. I have found that both nginx and unicorn are set up properly; however, on both machines my rails app does not want to start up in the production environment. The output of attempting to start up in production is:
/Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:47:in `resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:41:in `resolve_string_connection'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:25:in `spec'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:129:in `establish_connection'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railtie.rb:82:in `block (2 levels) in <class:Railtie>'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:26:in `block in on_load'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:25:in `each'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:25:in `on_load'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railtie.rb:74:in `block in <class:Railtie>'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `run'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `each'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/application.rb:136:in `initialize!'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/benvreed/Documents/rubydev/StudyPublic/config/environment.rb:5:in `<top (required)>'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in require'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
from /Users/benvreed/Documents/rubydev/StudyPublic/config.ru:4:in `block in <main>'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
from /Users/benvreed/Documents/rubydev/StudyPublic/config.ru:1:in `new'
from /Users/benvreed/Documents/rubydev/StudyPublic/config.ru:1:in `<main>'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:40:in `parse_file'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:200:in `app'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/server.rb:46:in `app'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/server.rb:70:in `start'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:55:in `block in <top (required)>'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:50:in `tap'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Although, I clearly do specify the postgresql adapter in my database.yml file:
# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
# gem install pg
# On Mac OS X with macports:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
development:
adapter: postgresql
encoding: unicode
host: localhost
database: study_development
pool: 5
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# The server defaults to notice.
#min_messages: warning
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: postgresql
encoding: unicode
host: localhost
database: study_test
pool: 5
production:
adapter: postgresql
encoding: unicode
host: localhost
database: study_production
pool: 5
What's really strange is that it successfully runs in both the development and test environments. I have checked other issues on StackOverflow and most have said that it is a spacing issue; however, I have redone the database.yml file several times with the same result.
My unicorn and nginx files are standard configurations.
Any assistance would be greatly appreciated. I am at a stand still until this runs. Thanks in advance.
RAILS_ENV
When using the Rails Console by executing RAILS_ENV=production rails c, running RAILS_ENV produces:
NameError: uninitialized constant RAILS_ENV
from (irb):1
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /Users/benvreed/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Rails.env produces:
=> "production"
The adaptor in you database.yml should be pg, not postgresql.
production:
adapter: pg
encoding: unicode
host: localhost
database: study_development
My setup uses this:
production:
adapter: pg
encoding: utf8
reconnect: false
database: my_app_name_production
Your database.yml settings is ok. Nothing weird.
If the development mode runs without exceptions, thus the problem is not in the DB.
Try to check database.yml precisely: maybe invalid/unacceptable* characters present.
some symbols are so similar :) с and c
Forking error!
Maybe some caching or log problems It shows that nothing of the errors in our code.
My team resolve it.
delete the project
git clone the project
config the database.yml
start server
Everything is okay!
The same project the errors gone!
So we think it is env problems or cach or log.
see here: (https://stackoverflow.com/posts/18695551/edit)