There was a thread yesterday about ENV["_"] which is not available to
Windows,
I got to playing around with how to find access to the
initializer_path or in this case the root_path
My question is more about why I don't have access to what appears to
be a public method than finding the root_path.
I did this in 2.0.2 with script/console
The Rails object has a Configuration class accessible via
Rails::Configuration
I have script console running and at the same time I am starring at
the code to initializer.rb
In initializer.rb is a class named Configuration. It has an accessor
class Configuration
# The application's base directory.
attr_reader :root_path
yet when I try to access this I get an undefined method error.
puts Rails::Configuration.root_path
NoMethodError: undefined method `root_path' for
Rails::Configuration:Class
from (irb):122
if I list the instance methods, root_path is listed, yet it is not
listed under
Rails::Configuration.public_methods.
Is Configuration a singleton class?
How do I access root_path or any of the other instance methods?
Thanks in advance.
In initializer.rb is a class named Configuration. It has an accessor
to :root_path
class Configuration
# The application's base directory.
attr_reader :root_path
yet when I try to access this I get an undefined method error.
puts Rails::Configuration.root_path
NoMethodError: undefined method `root_path' for
Rails::Configuration:Class
from (irb):122
Well yes, you're trying to call an instance method but you're calling
it on the class, not on an instance of it.
I don't think that instance is kept around in any accessible form
after the boot process, although I suppose you could stash it in a
constant in environment.rb. Why would you want to ?
Fred
I have always wanted to use that for site-wide configurations for
plugins (similar to how you store the SMTP configuration for
ActionMailer) in that configuration, but alas it seems to be not
accessible to do that. My ideas for things to include there are
Amazon S3 logins, e-commerce tokens and logins, etc. It just seems to
not be well suited for it (and not meant for it either). I know you
can put plain old constants there, but it would seem to be cleaner to
use some sort of configuration setup than just the equivalent of
"Here's a global variable" in environment.rb.
Thanks,
I guess I really don't need to do it so much as I a curious about how
it all works. The only real purpose is that the plugin in question was
looking for the initializer_path (dunno why) and that doesn't appear
to be available through ENV["_"] in a Windows environment. Rails
provides public access to the Rails.preinitializer_path, so it stands
to reason that things like Rails.root_path and initializer_path etc
might be handy at some point. I think I will see about adding them to
my Rails object as a pedantic exercise.