Search code examples
ruby-on-rails

Rails use of yml file


My first doubt is what is the difference between yml and yaml. Which one I should use. Also I have to put my label in yml file and to load them. So I don't have any idea how to do that. Any example or tutorial for that will be very helpful.


Solution

  • Setting Rails environment variables. Using ENV variables in Rails, locally and with Heroku. Rails configuration and security with environment variables.

    Environment Variables

    Many applications require configuration of settings such as email account credentials or API keys for external services. You can pass local configuration settings to an application using environment variables.

    Operating systems (Linux, Mac OS X, Windows) provide mechanisms to set local environment variables, as does Heroku and other deployment platforms. Here we show how to set local environment variables in the Unix shell. We also show two alternatives to set environment variables in your application without the Unix shell.

    Gmail Example

    config.action_mailer.smtp_settings = {
      address: "smtp.gmail.com",
      port: 587,
      domain: "example.com",
      authentication: "plain",
      enable_starttls_auto: true,
      user_name: ENV["GMAIL_USERNAME"],
      password: ENV["GMAIL_PASSWORD"]
    }
    

    You could “hardcode” your Gmail username and password into the file but that would expose it to everyone who has access to your git repository. Instead use the Ruby variable ENV["GMAIL_USERNAME"] to obtain an environment variable. The variable can be used anywhere in a Rails application. Ruby will replace ENV["GMAIL_USERNAME"] with an environment variable.

    Option One: Set Unix Environment Variables

    export GMAIL_USERNAME="[email protected]"
    

    Option Two: Use the Figaro Gem

    • This gives you the convenience of using the same variables in code whether they are set by the Unix shell or the figaro gem’s config/application.yml. Variables in the config/application.yml file will override environment variables set in the Unix shell.

    • Use this syntax for setting different credentials in development, test, or production environments:

      HELLO: world development: HELLO: developers production: HELLO: users

    In this case, ENV["HELLO"] will produce “developers” in development, “users” in production and “world” otherwise.

    Option Three: Use a local_env.yml File

    Create a file config/local_env.yml:

    Hope this help your answer!!!