How to disable caching in Rails? - ruby-on-rails

How can I disable caching for my rails site?
I'm running Passenger (mod_rails) and my site is running in 'development' mode:
'ENV['RAILS_ENV'] ||= 'development'
Any help?

By default Passenger will set RAILS_ENV to 'production'. The line ENV['RAILS_ENV'] ||= 'development' will only cause the Rails environment to be set to 'development' if it has not already set.
You need to add RailsEnv development to your virtual host configuration for the site to make Passenger run Rails in the development environment.

Okay, so I'll answer this question in case anybody else runs into the same problem. Basically, mod_rails was ignoring my 'development' setting that I had set in the environment.rb file. Adding this to my virtualhost configuration for my site fixed it, however:
RailsEnv "development"
See this link for more details.
Hope that helps somebody else!

Related

Rake not using the same environment value as phusion passenger setting

In my vhost, I have:
<Directory /var/www/prod/myapp/myapp/public>
...
RailsEnv production
...
</Directory>
and while any code dependent on it being production is correctly running there in the app itself (ex: display the Google Analytics code if Rails.env.production?), when I run rake about from /var/www/prod/myapp/myapp, I get:
Application root /var/www/prod/myapp/myapp
Environment development
Database adapter mysql2
which means that I have to prefix any deployment related rake stuff with RAILS_ENV=production. Granted, it's all in a deployment script at this point so it doesn't matter much, but why isn't Rake aware that it's production? Shouldn't the Passenger setting be enough? And if not, how do I fix it so I won't need to specify the environment manually?
Side-note: I am running the development instance of the app on the same box, with Environment set to development in its corresponding vhost configuration.
EDIT: Phusion Passenger version 4.0.20
rake is entirely unaware of passenger's configuration. It doesn't even know that you're using passenger. Since it isn't launched by passenger, it would have to (assuming it knew you were using apache/passenger) parse the apache config files to find this out, which would get pretty complicated, especially in the presence of multiple apps.
You could set this in one of your shell's startup files, however that doesn't sound like a good idea if you have multiple environments on the same machine.
You could stick
ENV['RAILS_ENV']='production'
At the top of one of Rails' startup files - boot.rb seems to do the trick. This would make passenger's setting ineffective though, and obviously you would only want to do this on the production deploy of your script.
Personally (especially on a machine with multiple environments in action) I'd stick to typing RAILS_ENV=production.
but why isn't Rake aware that it's production?
Because rake and all rails related commands internally do this
ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
which means that if you didnt have RAILS_ENV or RACK_ENV environment, then it will use development environment by default.
Shouldn't the Passenger setting be enough?
NO. Passenger setting is just for making your app up/live in desire or say virtual environment. It doesn't change any system configuration.
And if not, how do I fix it so I won't need to specify the environment manually?
That's very easy. Just set environment variable. If you are using bash (normally people do) you can simple do this
echo "export RAILS_ENV=production" >> ~/.bashrc ; source ~/.bashrc
This will make your server as rails production server, so if you run rails c or rake db:migrate etc or any rails or rake command it will run in production server for all available applications.
I am running the development instance of the app on the same box, with Environment set to development in its corresponding vhost configuration.
This is a problem. If you set the above environment variable, then whenever you run any code like rake db:migrate inside this app ..that will run in production environment. To avoid this either you dont set environment variable or specify environment on every command. There is one hack available . Include this line at the top of config/boot.rb
ENV["RACK_ENV"] = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"
Future reading : How can I make a custom environment in rails a default environment?

how to change rails to development mode

In the documentation of rails (3.2.3) it says
In development mode (which is what you’re working in by default), Rails reloads your application with every browser request, so there’s no need to stop and restart the web server.
But clearly my app loads in production mode out of the box.(I can type Rails.env and see it).
Why?
I then go to environment.rb and add
ENV["RAILS_ENV"] = "development"
and still it is in production.
Any idea?
Edit : Here you go
#Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
MyAppName::Application.initialize!
ENV["RAILS_ENV"] = "development"
Possible solution for your situation could be:
rails server -e development
Though this is not a solution try to start the Rails server this way:
RAILS_ENV=development bundle exec rails s
What do you see if put <%= Rails.env %> somewhere in you layout file?
If you are using Phusion Passenger, then add the following to your virtual host configuration file:
RailsEnv development
In other words, on my system, you would vim /etc/apache2/sites-available/[name of app] so that it looks like the following:
<VirtualHost *>
ServerName example.com
DocumentRoot /home/yourname/htdocs/example.com/public
RailsEnv development
</VirtualHost>
You would then need to restart the web server:
sudo /etc/init.d/apache2 reload
Credit goes to: http://my.opera.com/williamn/blog/2009/03/03/how-to-make-phusion-passenger-run-in-development-mode

rails running in staging on production machine

I have a machine where all my rails apps are running in a "staging" environment, even the production ones. It is trying to connect to the staging database. I have tried setting it set to production in the following places:
#/etc/apache2/mods-enabled/passenger.so:
# the line above was wrong, it is in /etc/apache2/sites-enabled/ier
RailsBaseURI /ier
RailsEnv production
<Directory /rails/production/ier/current/public>
Options -MultiViews
</Directory>
#app/controllers/application_controller.rb
Rails.env = 'production'
I even tried putting it in:
#config/environment.rb adding Rails.env = 'production'
I am getting a passenger error of:
staging database is not configured (ActiveRecord::AdapterNotSpecified)
I even ran ( grep -R "staging" . in ) and got:
Binary file ./.git/index matches
./config/deploy.rb:set :stages, %w(staging production)
./config/deploy.rb:# %w{staging.rb}.each do |config|
./config/deploy.rb:# %w{production.rb staging.rb}.each do |deploy_env|
./config/database.yml:staging:
grep: ./config/deploy/staging.rb: No such file or directory
grep: ./config/deploy/production.rb: No such file or directory
I am running Apache2, Passenger 3.0.7, ruby 1.9.2, and rails 3.0.9
Where else can I look to find out where I set the environment to staging?
thanks for any help
Try putting this inside your <VirtualHost> configuration (probably in /etc/apache2/site-enabled/000-default).
RailsEnv production
RackEnv production
I hear some versions of Passenger required RackEnv instead of RailsEnv so you might want to try that especially.
You can also try putting it in an .htaccess file in your public directory if you have AllowOverride set to Options or All.

How do I set RAILS_ENV to 'Production' on a DreamHost shared server?

I have a shared server running passenger to server my Rails app. For some reason my RAILS_ENV variable seems to be stuck as 'Development'. How do I set it to 'Production'? Thanks
In a shared hosting environment, you will want to do this in a .htaccess file, placed inside public/. Mine looks like this:
PassengerEnabled on
PassengerAppRoot /home/myuser/myapp/current
RailsEnv production

Ruby on Rails + Passenger, ENV not changing

First of all, our project structure is a bit different than a regular ruby on rails project therefore we don't have anything like environment.rb.
I am running into an issue changing the ENV variables. The server keeps starting under development mode.
Environment (value of RAILS_ENV, RACK_ENV, WSGI_ENV, NODE_ENV and PASSENGER_APP_ENV)
development
I tried changing those variables like this export RAILS_ENV=production and restarting the server using service httpd restart but for some reason none of these variables will change.
My question is, where can I possibly find/override those variables in a file?
Try setting the environment variables in the /etc/environment file on your server. I had a similar issue recently and that seemed to resolve it for me. It would look something like FOO=bar
Found it. It was under my httpd.conf where I just changed variable RailsEnv to production
In Rails 5, these lines are moved from .htaccess to apache configuration file.
RackBaseURI /
PassengerAppRoot /path/to/public_html
RailsEnv production
I am running on apache 2.4.18, Phusion Passenger 5.2.1.

Resources