Rails 3 - set environment - ruby-on-rails

I have a rails 3 app (which I upgraded). It runs on passenger and nginx but on my production server it also starts with the environment set to 'production'. I know I am missing something really basic, but I just can't figure out where to set the environment instead of in environment.rb.
Thanks for helping!
UPDATE: ok, I learned I can still do that with Rails.env = 'production'.
That seems kind of old school to me. Do you know an elegant way to configure this maybe in the Capfile or sth like that?

Rails 3 is a little bit different than Rails 2.x in that it has a config.ru file, like other Rack applications.
Passenger detects rails as a Rack app, so you'll have to use RackEnv instead of RailsEnv in the vhost. You can set the environment using RackEnv as per the documentation for Passenger/Nginx.

You can configure a different RAILS_ENV for each app in your vhost for nginx with passenger. I've never used nginx but in apache it's just a RailsEnv=development directive. That way each site just has it set, no worries with configuring a cap task or variable or anything. See the docs. Note that the default is production so this should already be set for you.

Related

What does RACK_ENV do in a Rails application?

I have a Rails application already in production. The guy before set these environment variables:
...
export RACK_ENV=none
export RAILS_ENV=production
...
What does RACK_ENV=none do? I can't find documentation on it anywhere. Do I need to set it in the Rails application or can I just delete that export?
IMHO it's useless.
To find the current environment a Rails app first looks for the RAILS_ENV environment variable, then for RACK_ENV environment variable, then it defaults to 'development'.
If you're using version 1.7 or later of the database_cleaner gem, and your CI server has RACK_ENV set to production like mine did, you'll need to set RACK_ENV to none (or anything other than production) to appease database_cleaner's safeguard that your tests aren't running in production. (Or you could disable the safeguard altogether, but that seems less safe.)
Looking at current rack source, it appears that the only value of RACK_ENV that is meaningful to rack is development, which causes rack to default the host to localhost instead of to 0.0.0.0. So it's foolish to set RACK_ENV to production in the first place, or to check that it's been set to that, but that foolishness has taken root all over.

YAML anchors issue

Here is the tutorial I'm following: http://rhnh.net/2011/01/31/yaml-tutorial
I'm using the SettingsLogic gem: https://github.com/binarylogic/settingslogic
And every config file used by Settings logic works fine, but if I use the same tactics on database.yml or thinking_sphinx.yml it will not work.
My bad I didn't specified full stack info.
I'm using passenger. But I missed directive RailsEnv in apache config file.
http://www.modrails.com/documentation/Users%20guide%20Apache.html#rails_env
By default, passenger loads production environment.
Setting RailsEnv staging solved my problem. And now correct settings picked.

Changing the base URL for Rails 3 development

I know I'm going to deploy to an environment with my application running with a base URL which looks like this:
http://someserver/mydepartment/myapp
My development environment is set up to use the default Rails configuration, which looks like this:
http://localhost:3000/myapp
I'd like to model this deployment path in my development environment. That is, I'd like to develop with a base URL which looks like this:
http://localhost:3000/mydepartment/myapp
That way, I can make all my URLs relative to "/" and they will work in both environments.
How can I change it so my application will live at this path in my development environment?
Solutions I've found, but don't work for me:
Setting the scope in routes.rb doesn't seem to work for the static content in public.
Using Apache's rewriting capabilities. I don't want to install Apache on my development box. Ideally the solution would work with WEbrick, though I seem to have Mongrel mostly working as well (there are some problems with Mongrel and Ruby 1.9.2).
Setting relative_url_root and similar suggestions which don't work with Rails 3.
Dynamically generating CSS/JavaScript and adjusting the paths to compensate between development and production environments.
You can try mapping your rails app rack config to a different base_uri.
All you need to do is wrap the existing 'run' command in a map block
try doing this in your rails 'config.ru' file:
map '/mydepartment' do
run Myapp::Application
end
Now when you 'rails server' the app should be at localhost:3000/mydepartment .
Not sure if this will give you the desired outcome, but worth a try.
Here’s how you can deploy a Rails 3.1 app to a subdirectory in Apache, replacing config.action_controller.relative_url_root which no longer exists.
In config/routes.rb:
scope 'my_subdir' do
# all resources and routes go here
end
In your Apache configuration file:
Alias /my_subdir /var/www/my_subdir/public
<Location /my_subdir>
SetEnv RAILS_RELATIVE_URL_ROOT "/my_subdir"
PassengerAppRoot /var/www/my_subdir
</Location>
And it should work, including automatically pointing all your assets to /my_subdir.

Environments in Rails

I have read that deploying an application on development environment may be one of the worst cases, but i couldn't find any real information about HOW i can change the environments of my applications and make their production databases ready?
I am using Passenger/Nginx for deployment by the way.
Edit : People you get it wrong, maybe i asked wrong, i know how to change environments by nginx, but if i change it from nginx and don't touch to my app, it crashes. There are some things i have to done to my app before i change their environment from development to production, i want info about them.
In your virtual host put following environment variable:-
<VirtualHost *:80>
DocumentRoot /var/apache2/htdocs/tutorial/Web/
ServerName dev.tutorial.local
SetEnv FLOW3_CONTEXT Production
</VirtualHost>
http://www.modrails.com/documentation/Users%20guide%20Nginx.html#RailsEnv
From the Passenger documentation, RAILS_ENV defaults to production.
If not you can specify it in the nginx configuration:
In the http configuration block.
In a server configuration block.
In a location configuration block.
In an if configuration scope.
I am not sure. but try changing environment varaible in config/environment.rb
ENV['RAILS_ENV'] ||= 'production'

Ruby on Rails: How to set which development environment an app runs in?

I'm relatively new to Ruby on Rails and occasionally I find this convention-over-configuration stuff a little confusing as a lot of things seemed to be hidden from the developer, as in this case.
I'm using rails 2.3.8 and when I run my app locally through NetBeans 6.9/Mongrel on my system it runs using the development environment parameters.. when I deploy it to a Fedora box and run it there in Apache HTTPD it automatically runs using the production environment parameters.
How does my app know which environment to use? I haven't changed anything in my app to set the environment.. both versions locally and on my Fedora box are identical. I can't find anywhere in the code where it is setting the environment.. so how is this working?
Thanks.
In httpd.conf file, write the following in VirtualHost:-
## Specify Rails Environment here,
default value is "production"
RailsEnv development
Thanks...
The primary way to specify rails mode is RAILS_ENV environment variable (I assume development is default, when nothing is specified). You can check its value in bash, echo $RAILS_ENV.
You can also modify ENV['RAILS_ENV'] in your config file to change the mode:
ENV['RAILS_ENV'] = 'production'
edit
I've never used rails with apache, but I think passenger mod can also specify this variable somewhere, checking apache configs might help.

Resources