How to Make a Custom Environment in Rails a Default Environment

How can I make a custom environment in rails a default environment?

Ideally you have to set environment variable in .bashrc like

  export RAILS_ENV=staging

because rails is fully dependent on environment variable. But like you said

adding RAILS_ENV in ~/.bashrc or ~/.bash_profile file of the user. will make this application depent on the console, shouldn't it just work independent of ~/.bashrc or ~/.bash_profile file?

Obviously, this is another option. Include this line at the top of config/boot.rb

ENV["RACK_ENV"] = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "staging"

This will work everywhere. I have tested in following places

  1. Rails 4
  2. Rake
  3. Server
  4. Console
  5. dbconsole
  6. It will pick the environment if it is set in bashrc or zshrc etc.

How to create a new environment in Ruby on Rails?

Assuming you want create the hudson environment.

  1. Create a new environment file in config/environments/hudson.rb. You can start by cloning an existing one, for instance config/environments/test.rb.
  2. Add a new configuration block in config/database.yml for your environment.
  3. Update any other configuration file you might have under the config folder with your new environment, some gems create their own ymls under the config folder.
  4. That's all.

Now you can start the server

rails server -e hudson

or run the console

rails console hudson

And so on.

If you are using an older version of Rails, the commands are

ruby script/server -e hudson
ruby script/console hudson

Rails console default environment

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 3 / Setting Custom Environment Variables

You can do this with initializers.

# config/initializers/configuration.rb
class Configuration
class << self
attr_accessor :json_url
end
end

# config/environments/development.rb
# Put this inside the ______::Application.configure do block
config.after_initialize do
Configuration.json_url = 'http://test.domain.com'
end

# config/environments/production.rb
# Put this inside the ______::Application.configure do block
config.after_initialize do
Configuration.json_url = 'http://www.domain.com'
end

Then in your application, call the variable Configuration.json_url

# app/controller/listings_controller.rb
def grab_json
json_path = "#{Configuration.json_url}/path/to/json"
end

When you're running in development mode, this will hit the http://test.domain.com URL.

When you're running in production mode, this will hit the http://www.domain.com URL.

Separate environment variables into different environments

Found the answer in some other posts dealing with environments

default: &defaults
# Environment variables

development: &development
<<: *defaults
production:
<<: *development

How does a Rails app set its Environment (Dev., Staging, etc.)?

Finally figured it out today.

Since adding the staging environment to the application I guess I needed to add some config files inside a puma directory in the app itself to help puma figure out what it needed to do.

To be honest I'm still not sure why I needed this since puma would say it was running in development, which I thought was setting the environment.

What I did was add a puma directory inside the config directory and then I added a file named development.rb and in there put the setting for running in the development environment.

Content of puma/development.rb:

#!/usr/bin/env puma
root = "/Path/to/the/application"
daemonize false
environment "development"
directory root
pidfile "#{root}/tmp/pids/puma.pid"
stdout_redirect "#{root}/log/puma_stdout.log", "#{root}/log/puma_stderr.log", true
workers 2
threads 8,32
bind "unix:///#{root}/tmp/sockets/puma.sock"
bind "tcp://0.0.0.0:8080"
preload_app!

It nows runs as expected on my local machine.

How can I set the Rails environment for my somewhat stand alone Ruby script?

If you're going to be using the rails environment, your best bet would be to make this a rake script. To do this, put a twitter.rake file into lib/tasks and begin and end it like this:

task(:twitter_load => :environment) do
# your code goes here
end

That way, you're doing it by "following conventions" and it doesn't have that 'orrible smell associated with it.

Rails initializer for development and production

I would create a config file for this (config/chargify.yml):

development:
subdomain: example
api_key: 123abc
production:
subdomain: production_domain
api_key: 890xyz

And then change your Initializer like this:

chargify_config_file = File.join(Rails.root,'config','chargify.yml')
raise "#{chargify_config_file} is missing!" unless File.exists? chargify_config_file
chargify_config = YAML.load_file(chargify_config_file)[Rails.env].symbolize_keys

Chargify.configure do |c|
c.subdomain = chargify_config[:subdomain]
c.api_key = chargify_config[:api_key]
end

Are you using custom rails environments?

It depends if your "staging" environment is a different thing or is actually a variation on the "production" environment as is usually the case.

Normally you just deploy in full production mode, using the production.rb file and everything, to a non-production server. This distinction is irrelevant to rails, and matters only to your deployment script. For example Capistrano would be concerned about this.

The only time you need another environment is if you need a different group of settings for Rails.

How to check rails environment?

One liner if you are in app root

rails r "puts Rails.env"



Related Topics



Leave a reply



Submit