rails/rails — Issue #3259: Asset precompilation does not work when deploying under sub-path
Author: markembling
Created: 2011-10-08 18:07:07+00:00
State: open

### Overview

When deploying a Rails application (3.1.1) to a sub-path under Passenger, there is no way to provide this sub-path when precompiling assets. The result of this is that SCSS files have incorrect paths in them to images etc when the `image-url` and other helpers are used.

### Brief Steps to Reproduce

Production environment is set up as follows for Apache/Passenger:

# Rails applications

RailsBaseURI /sample

Options -MultiViews

RailsBaseURI /another-app

Options -MultiViews

**Unfortunately the apps this affects (for me) are not public (neither the source or the running app), so I can't link directly to it. However it will be a problem common to all Rails apps deployed to a sub-path under Passenger using capistrano. The following steps (assuming a new app) should (hopefully) reproduce the problem.**

Add a background image to `app/assets/images` and add the following to `application.css.scss`:

body { background-image: image-url("bg.png"); }

Make sure the application is to be deployed using capistrano:

`Gemfile`:

# Deploy with Capistrano
gem 'capistrano'

`Capfile`:

load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }

load 'config/deploy' # remove this line to skip loading any of the default tasks
load 'deploy/assets'

`config/deploy.rb`

require "bundler/capistrano"

set :application, "SampleApp"
set :repository, "git@git.example.com:SampleApp"
set :scm, :git
set :branch, "master"

set :deploy_to, "/var/rails_apps/example"
set :user, "mark"
set :deploy_via, :copy

role :web, "server.example.com" # Your HTTP server, Apache/etc
role :app, "server.example.com" # This may be the same as your `Web` server
# Commented out because its irrelevant to the sample.
#role :db, "your primary db-server here", :primary => true # This is where Rails migrations will run
#role :db, "your slave db-server here"

default_run_options[:pty] = true

# If you are using Passenger mod_rails uncomment this:
# if you're still using the script/reapear helper you will need
# these http://github.com/rails/irs_process_scripts

_cset :asset_env, "RAILS_GROUPS=assets"
_cset :assets_prefix, "assets"

namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
end

### End Result

When the application is deployed, the assets will be precompiled on the server and the resulting stylesheet when sent to the browser will be missing the sub-path.

Desired: `background-image:url(/sample/assets/bg-.png)`
Actual: `background-image:url(/assets/bg-.png)`

This is expected, because the precompilation process is not happening from within the context of the running application under Passenger, thus the sub-path is not known. The problem is that there seems to be no way to get this information into it.