0

My System is macOX Mojave 10.14
MySQL is MySQL:8.0.16
My database.yml is:

development:
  adapter: mysql2
  encoding: utf8
  database: dev_database
  reconnect: false
  pool: 5
  username: <%= ENV['RAILS_DEV_DB_USSERNAME'] %>
  password: <%= ENV['RAILS_DEV_DB_PASSWORD'] %>
  socket: /tmp/mysql.sock

and I have confirmed if the variable is valid. Using

Command-Line: erb config/database.yml

I can get:

development:
  adapter: mysql2
  encoding: utf8
  database: dev_database
  reconnect: false
  pool: 5
  username: root
  password: Wle3S#23sv
  socket: /tmp/mysql.sock

But when I start rails s -e developent, and browse to my page, I can not connect to my database.

enter image description here

What can I do now?

Vasilisa
  • 4,604
  • 3
  • 20
  • 25

1 Answers1

0

As Author arpiagar suggested rails 3, how use an ENV config vars in a Settings.yml file? check his example (26)

when you introduce a scriptlet tag in .yml file, it is more of erb template. So read it as a erb template first and then load the yml

You can use dotenv or figaro gems (inspired by the Twelve-Factor App methodology) to loads environment variables.

For example if you use dotenv gem

By default, load will look for a file called .env in the current working directory. Pass in multiple files and they will be loaded in order. The first value set for a variable will win.

require 'dotenv'
Dotenv.load('file1.env', 'file2.env')
HOSTNAME = ENV['HOSTNAME']

More info please check below mentioned links

  1. http://railsapps.github.io/rails-environment-variables.html

  2. https://www.honeybadger.io/blog/ruby-guide-environment-variables/

amit bhosale
  • 482
  • 4
  • 9