sanny Sin
sanny Sin

Reputation: 1555

Configuring variables depending on current Rails.env

What would be the most simple way to configure different variables(for ex. i have variable in tag link_to, which changing, depending on current Rails.env) ?

<%
    if Rails.env == 'development'
        domain = 'somedomain.me'
    elsif Rails.env == 'staging'
        domain = 'example.com'
    end 
    %>
    <%= link_to 'Back', root_url(:subdomain => 'www', :domain => domain), :class=>"btn btn-primary"%>

I want to move this if - elsif statement to somewhere else, or even drop it for some kind of 'configuration files' if any exists

Upvotes: 4

Views: 1653

Answers (1)

Castilho
Castilho

Reputation: 3187

There's an awesome gem for just this, called rails_config: github repo

But if you just want to configure this and only this variable, you could create a constant at the environment configuration files that rails already has.

Something like:

In your config/environments/production.rb

# production.rb
Rails.configuration.my_awesome_changing_domain = "somedomain.me"

In your config/environments/development.rb

# development.rb
Rails.configuration.my_awesome_changing_domain = "stackoverflow.com"

In your config/environments/staging.rb

# staging.rb
Rails.configuration.my_awesome_changing_domain = "news.ycombinator.com"

Other methods to do that are discussed in another answers, at this thread: https://stackoverflow.com/a/5053882

Best regards

Upvotes: 4

Related Questions