Search code examples
ruby-on-railsproductionmode

How to set up a complete "hello world" site with rails 3.1 in production mode?


I'm struggling with setting up a rails-based web site in production mode. An intermediate goal would be to set up a very simple web site. I'm using Rails 3.1.0, rake 0.9.2.2, and Ruby 1.9.2.

Here is what I tried, with unsuccessful results so far:

initially, make sure RAILS_ENV is not set

rails new test_project

cd test_project

rails generate scaffold User name:string email:string

rake db:migrate

rails s

browse to localhost:3000 or localhost:3000/users

things look OK.

now, attempt to set this up for production:

export RAILS_ENV=production

rake db:migrate

rake assets:precompile

rails s

browse to localhost:3000

PROBLEM: Routing Error; No route matches [GET] "/"

kill rails

in config/routes, add root :to => 'users#index'

rails s

can now browse to localhost:3000 and localhost:3000/users

BUT, rails generates the following errors:

Started GET "/assets/application-00960e5186894b532975562d59176a6a.css" for 127.0.0.1 at 2011-11-26 23:09:44 -0800

  ActionController::RoutingError (No route matches [GET] "/assets/application-00960e5186894b532975562d59176a6a.css"):

  Started GET "/assets/application-ae30e133eabbb10d9464189d3fb71e25.js" for 127.0.0.1 at 2011-11-26 23:09:44 -0800

  ActionController::RoutingError (No route matches [GET] "/assets/application-ae30e133eabbb10d9464189d3fb71e25.js"):

Can anyone shed light on how to fix the above simple attempt to get a Rails 3.1 project working in "production" mode?


Solution

  • The fundamental problem is that the default configuration for running an application in "production" mode makes some assumptions about deployment -- primarily that you're using another webserver (eg. nginx, apache) for serving static assets. You're getting the file not found errors because by default Rails will not serve static assets in production mode

    If you're trying to use WEBrick to duplicate a "production" environment, it needs to be configured to serve static assets. You can simply flip the boolean in your production.rb

    environments/production.rb # Disable Rails's static asset server (Apache or nginx will already do this) config.serve_static_assets = false

    Once you make that change and restart the server, you'll be serving the assets you precompiled using WEBrick which while inefficient, will certainly give you an idea about what it's like in production.