We recently ran into an issue precompiling assets in Rails 3.1 for production. By default, Rails 3.1’s rake:assets:precompile task jams assets into a /assets directory. This works for simple deployments, but when deployed in Tomcat (or most application containers) some root context is applied to the application path.

nn

For example – when running against rails server the URL to your application might look something like:

nn

http://localhost:3000/users

nn

When deployed, it might look something like:

nn

http://localhost:10000/my-awesome-app/users

nn

What you’ll find is your deployed app can’t find any assets since it’s looking in /assets and not /my-awesome-app/assets. You can read more about the issue here.

nn

A quick solution is to use the rake task suggested by atambo that forces a relative path for your assets directory (from the issue thread):

nn

[ruby]n namespace :assets don   desc "Make all embedded assets in stylesheets relative paths"n   task :relativize => :environment don     Dir[Rails.root.join("public/assets") + "*.css"].each do |asset|n       contents = File.read(asset)n       File.open(asset, "w") do |file|n         file.puts contents.gsub("url(/assets/", "url(")n       endn     endn   endn endn [/ruby]n

nn

Add this rake task to your Rakefile and make sure to run it after precompiling your assets. It’s not ideal, but it’s a simple workaround for an issue that exists in Rails 3.1. It appears to have been fixed in 3.2 but updating Rails was not an option for us at the time.

n