Accessing config from application.rb in Controller (Rails 3)
I believe you've got a slightly incorrect idea behind what your expectations for the config/application.rb is providing you. The ActiveRecord::Base and ActiveController::Base eigenclasses use the Rails::Application::Configuration class that is configured in config/application.rb. The attributes aren't available in classes that descend from either of the Base classes, nor their eigenclasses. This is why you are running into errors in ApplicationController.
There are generally two ways to make configuration initializations in a Rails app. The first way is to create a configuration module and then load values into it via initializer:
First, create a Twiter Config module:
#lib/twitter_config.rb
module TwitterConfig
def self.config
@@config ||= {}
end
def self.config=(hash)
@@config = hash
end
end
Create a YAML config file:
# config/twitter.yaml
development: &base
key: "foo"
secret: "bar"
test:
<<: *base
key: "foo2"
production:
<<: *base
secret: "barbar"
Alternatively, if you don't intend to add config/twitter.yaml to your SCM, you can just skip this and set the key and secret via environment variables. This would be the suggested solution for an application with a public SCM repository deploying on Heroku.
Then load and set the value via an initializer:
#config/initializers/01_twitter.rb
require 'twitter_config'
TwitterConfig.config = YAML.load_file("config/config.yml")[Rails.env].symbolize_keys
It's generally a best practice to number your initializer files as Rails will load them in order according to their filename. If you are initializing a datastore and that is critical for other steps, then it needs the lowest number. Alternatively, if you are using environment variables, this would be the init file:
#config/initializers/01_twitter.rb
require 'twitter_config'
TwitterConfig.config[:key] = ENV['twitter_config_key']
TwitterConfig.config[:secret] = ENV['twitter_config_secret']
Throughout the Rails application, you now have access to the config values with TwitterConfig.config[:key] & TwitterConfig.config[:secret]. You can include the module as well, just watch out for conflicts.
You can also just load the values as a global constant. It feels a bit ugly to me though:
#config/application.rb
TWITTER_CONFIG = YAML.load_file("config/twitter.yaml")[Rails.env]
How to read ruby on rails config values from within the application controller
The class method ActionController::Base.session_options
returns a hash of the configuration options for the session.
So in your case you want ActionController::Base.session_options[:domain]
How do I access a Rails configuration value during runtime?
For Rails 2, you can do:
Rails.configuration.cache_classes
If you ever switch to Rails 3, it'll be different; you can access the same value with:
Rails.application.config.cache_classes
How to access configuration flag (set from config/environments/development.rb) from a controller?
The config object referred to in environments/*.rb is accessible by
Rails.application.config
Update rails config file from Controller
IMHO it's should be avoided. If nature of your config is dynamic (changes in time) then you should just read it from DB as you read User data or whatever.
If it's static then you may keep it in hash (YAML file, when config changes you just deploy your app). If settings change frequently I would recommend db.
Rails 3 Migration and application.rb
Trying renaming your app/controllers/application.rb
to application_controller.rb
.
I think Rails is expecting your controller to be named with a _controller
suffix, and the application.rb
you have in your controllers folder isn't following that convention.
Rails 3: application.rb not loading?
Is the AuthenticatedSystem
module mixed in to your application_controller
?
If so, then methods there will not be automatically available in the views.
You need to add something like:
helper :check_author_role
... in your application_controller
, after mixing in the AuthenticatedSystem
module.
How to get variable in sorcery.rb at controller
If you have an instance of User
you can do it this way:
User.new.sorcery_config.activation_token_expiration_period
How to define custom configuration variables in Rails?
Update 1
Very recommended: I'm going with Rails Config gem nowadays for the fine grained control it provides.
Update2
If you want a quick solution, then check Jack Pratt's answer below.
Although my original answer below still works, this answer is now outdated. I recommend looking at updates 1 and 2.
Original Answer:
For a quick solution, watching the "YAML Configuration File" screen cast by Ryan Bates should be very helpful.
In summary:
# config/initializers/load_config.rb
APP_CONFIG = YAML.load_file("#{Rails.root}/config/config.yml")[Rails.env]
# application.rb
if APP_CONFIG['perform_authentication']
# Do stuff
end
Related Topics
M Hartl's Ruby on Rails Tutorial Chapter 5 Custom Title on Home Page
Convert String to Symbol-Able in Ruby
Stop Rails Console from Printing Out the Object at the End of a Loop
Find Out If an Ip Is Within a Range of Ips
How Much Performance Do You Get Out a Heroku Dynos/Workers
Puppet/Facter "Could Not Retrieve Fact Fqdn": How to Fix or Circumvent
How to Get Constants Defined by Ruby's Module Class via Reflection
Implementation of "Remember Me" in a Rails Application
Creating Signature and Nonce for Oauth (Ruby)
How to Use Ruby's Readlines.Grep for Utf-16 Files
Ruby Array to String Conversion
Installing Ruby 1.9.1 on Ubuntu
How Would You Test Observers with Rspec in a Ruby on Rails Application
Rspec: How to Test File Operations and File Content
How to Check to See If a File Exists (On the Remote Server) in Capistrano