Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Rails.env and Config.root work in 'rails console' model?

Both objects seem to be nil inside of 'rails console'.

Why is that?

Actually now Rails works:

puts Rails.env
/path/to/my/project

but for config:

puts config.root

NameError: undefined local variable or method `config' for #<Object:0x1001dd2a0>
    from (irb):8
like image 839
Blankman Avatar asked Nov 22 '10 02:11

Blankman


People also ask

Where are Rails environment variables stored?

Keeping Environment Variables PrivateRemote git repositories such as GitHub are a place to store and share code. If your project is open source, any developer will have access to your code. You don't want to share email account credentials or private API keys with the public.

What is config in Rails?

In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The configuration file config/application. rb and environment-specific configuration files (such as config/environments/production.

Where is Rails application config?

You probably know that you can configure Rails in config/application. rb and config/environments/development. rb etc. But you can also leverage that for configuring your own custom settings for your application.

How do I check my current Rails environment?

The Rails environment is determined by whatever the value of the RAILS_ENV environment variable is when the server starts. You might have some configuration file somewhere that specifies it, or maybe you just start your server with a command of the form RAILS_ENV=production my_rails_server ?


1 Answers

There is no global config method. If you're copying this from your environment files in Rails 2, note that these are actually evaluated within a certain context.

To get to the config object from your console, try one of the following:

For Rails 3 and above:

Rails.application.config

For Rails 2.x:

Rails.configuration

If you need to get your project's root directory (not sure if this is actually what you're trying to accomplish), you can do the following in Rails 2 and above:

Rails.root
like image 104
molf Avatar answered Sep 29 '22 12:09

molf