Vlad
Vlad

Reputation: 8268

How to access configuration flag (set from config/environments/development.rb) from a controller?

I am trying to use the "config.consider_all_requests_local" flag to generate dynamic error pages only when it's in production mode.

I've set the config/environments/development.rb file with the following code:

config.consider_all_requests_local       = true

And inside app/controllers/application_controller.rb I added this line. (and :render_error, :render_not_found methods which I didn't include below)

unless config.consider_all_requests_local
       rescue_from Exception, :with => :render_error 
       rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
       rescue_from ActionController::RoutingError, :with => :render_not_found
       rescue_from ActionController::UnknownController, :with => :render_not_found
end

Anyway it seems that inside application_controller.rb, the flag is always false. The unless clause gets called all the time. I've been trying to figure this out, assuming that the flag would have been automatically set to true because of the config/environments/development.rb file.

I tested by adding the flag declaration right above the unless clause, and that seems to affect whether the unless clause gets called or not. So I'm guessing that either the flag is not being set inside config/environments/development.rb or it's not visible from inside app/controllers/application_controller.rb

Anyone know what the problem is? Thanks!

Upvotes: 0

Views: 872

Answers (1)

xyz
xyz

Reputation: 1497

The config object referred to in environments/*.rb is accessible by

Rails.application.config

Upvotes: 2

Related Questions