Rails Send_File in Production Delivers an Empty File

If you’re running Rails in production it will by default be configured to let apache or nginx send files for you. If you’re handling file downloads yourself with send_file

send_file("path/to/file.txt")

you will notice that the downloaded files are empty. To get around this just comment out the following in your config/environments/production.rb file.

  # comment out for production because apache/nginx are not doing this for us
  #config.action_dispatch.x_sendfile_header = "X-Sendfile"

Ruby Enterprise Edition and Passenger ./script/console production fails and instead returns Loading production environment (Rails 2.3.5) Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org

After installing Ruby Enterprise Edition, REE, and Passenger on Ubuntu you may see this error message when you run script/console for the first time

./script/console production
# =>
Loading production environment (Rails 2.3.5)
Rails requires RubyGems >= 1.3.2. Please install RubyGems and try again: http://rubygems.rubyforge.org

You then scratch your head and run

which gem
which ruby
which rails

to find that all appears to be in order. You have rubygems installed , ruby is installed and so is rails. You also find that each are pointing to the correct location, which is something like /usr/bin/gem -> /opt/ruby-enterprise-x.x.x.x/bin/gem, where x.x.x.x is the version of REE.

The problem isn’t however, with any of the above. The issue is with the location of irb. If you installed (like me) irb with apt-get install irb, then irb isn’t aware of your shiny new REE and ruby gems. It’s a simple fix however, unlink irb and symlink the /usr/bin/irb to REE’s irb like so…

rm /usr/bin/irb

And symlink it to the irb that REE has in bin

ln -s /opt/ruby-enterprise-x.x.x.x/bin/irb /usr/bin/irb

Now cd into your rails app and run

./script/console production

Defining Application Constants for Ruby on Rails Application

The best place to keep application constants which are environment specific is in config/environments directory. For instance…

# in RAILS_ROOT/config/environments/development.rb
APP_DOMAIN = "localhost"
# in RAILS_ROOT/config/environments/production.rb
APP_DOMAIN = "real-domain.com"

…will set the APP_DOMAIN constant to either “localhost” or “real-domain.com” depending on which environment Rails boots up.

Rails: Expiring a cached page with namespaces and sweepers

I’ve got some pages that are cached using their permalinks on the filesystem, such as http://example.com/about-us.html which will need to map to RAILS_ROOT/public/about-us.html … The issue I have is that I use a namespace for the admin area and the controllers in the namespace are responsible for expiring the cached pages, i.e., when the resources are updated by an admin.

Check out Rails Envy for a great tutorial for getting page caching set up: http://www.railsenvy.com/2007/2/28/rails-caching-tutorial

So what I want to do is expire the pages from inside my namespace. To accomplish this I need to use the pages route in my sweeper class.

class PageSweeper < ActionController::Caching::Sweeper
  #... after_save, after_destroy... we'll exprire the cache
  def expire_cache_for(record)
    #permalink rather than record id like /213.html
    expire_page(pages_path(record.permalink))
  end
end

Since I’m using a permalink to cache the page, I need to expire it with the permalink too.

Sample Rails Database Config for MySQL

Sample Ruby on Rails database config file for connecting to mysql.

production:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: db_production
  pool: 5
  username: db_user
  password: db_password
  #socket: /tmp/mysql.sock #this may vary

upgrading to latest phusion passenger 2.1.2

super easy

gem install passenger
passenger-install-apache2-module

will walk you through the install and remember to copy paths to your apache config file. for passenger i kept it in
mods-available/passenger.conf and then linked it to mods-enabled

ln -s /etc/apache2/mods-available/passenger.conf /etc/apache2/mods-enabled/passenger.conf

then just restart apache

more info and official docs at http://blog.phusion.nl/2009/03/13/phusion-passenger-212-final-released/