Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7602189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:20:56+00:00 2026-05-30T23:20:56+00:00

I currently have a Rails 3.1 app that hosts multiple sites for several different

  • 0

I currently have a Rails 3.1 app that hosts multiple sites for several different customers.
Each site is represented by a model that knows the domain name and path to where the assets are stored, along with some other info.

I’ve been using app/sites/domain-name as a location for storing assets, views and locales that are specific to a given site, and are running a custom middleware and controller actions for modifying the load path for Sprockets, setting up the view paths and so on.

middleware (loaded using config.middleware.use "Configurator" in application.rb):

class Configurator
  def initialize(app)
    @app = app
  end

  def call(env)
    @env = env
    clear_existing_paths
    prepend_local_assets
    @app.call(env)
  end

  def current_site
    # find site using @env
  end

  def current_site_path
    Rails.root.join('app', 'sites', current_site.path)
  end

  def clear_existing_paths
    paths = Rails.application.assets.paths
    Rails.application.assets.clear_paths
    paths.each do |path|
      next if path.include?("app/sites/")
      Rails.application.assets.append_path path
    end
  end

  def prepend_local_assets
    path = current_site_path.join('assets')
    return unless Dir.exists?(path)

    ['images', 'javascripts', 'misc', 'stylesheets'].each do |subdir|
      asset_dir = path.join(subdir).to_s
      next unless Dir.exists?(asset_dir)
      next if     Rails.application.assets.paths.include? asset_dir

      Rails.application.assets.prepend_path asset_dir
    end
  end
end

Asset bits from application.rb:

module MyApp
  class Application < Rails::Application
    ...
    config.middleware.use "Configurator"

    config.assets.enabled = true 
    config.assets.version = '1.0'
  end
end

And environments/production.rb:

MyApp::Application.configure do
  config.serve_static_assets = false
  config.assets.compress = true
  config.assets.compile = true
  config.assets.digest = true
  # Defaults to Rails.root.join("public/assets")
  # config.assets.manifest = YOUR_PATH
end

The problem is that while this setup works, it prevents me from precompiling the assets that is not shared with the entire app, making them be generated over and over again, from the looks of it.

Is there any way I could tell the precompiler to find the assets located here, and create a version of those as well? Each site has a site.css.scss and a site.js.coffee file that might require other assets inside the site-dir. Would be nice if I could get it precompiled to public/assets/domain-name/site.(js|css), so I could easily set up a separate subdomain for assets down the line when I need to optimize further

Final solution (2012-02-22)

After implementing what was suggested by Brian, I have ended up with

Main stylesheet/javascript stored in app/sites/<sitename>/assets/<shortname>/site.css|js, where sitename is the domain for this site, and shortname is the main part of the domain, with no subdomain or com|org|net|ccTLD.

Modified all views and stylesheets to prepend shortname to my asset paths.

In config/application.rb:

{ "sitename" => "shortname", ... }.each_pair do |domain, short|
  %w{stylesheets javascripts}.each do |dir|
    config.assets.paths << Rails.root.join("app/sites/#{domain}/assets/#{dir}").to_s
  end # Had to specify each dir to make it work
  config.assets.precompile += ["#{short}/site.css", "#{short}/site.js"]
end

When running rake assets:precompile this creates public/assets/shortname filled with all the assets for that site, and public/assets have all the shared assets as well. Works great for my needs.

And since everything ended up in public/assets, I was able to drop the Configurator-middleware, since the default configuration was able to find all the assets

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:20:57+00:00Added an answer on May 30, 2026 at 11:20 pm

    I think the problem is, by adding each site as a path, sprockets only finds the first site.scss

    I’ve tried this using compass, but it should be the same for plain sprockets.
    I haven’t tried your configurator approach, but it looks straightforward to adapt my arrangement to it.

    Can you change your app/sites/* to the more standard file arrangement?

    ./app/assets/javascripts/application.js
    ./app/assets/stylesheets/screen.css.scss
    ./app/assets/stylesheets/othersite/screen.css.scss
    

    Change your config/application.rb, and add each of your sites.
    This will pregenerate all styles, on each of your hosts:

    config.assets.precompile += ['screen.css', 'othersite/screen.css']
    

    In your view/layouts/application, you’ll need to configure the path to sitename:

    = stylesheet_link_tag '[your sitename here]/screen'
    

    After I rake assets:clean and precompile, I see this in public:

    ./assets/othersite/screen.css
    ./assets/othersite/screen.css.gz
    ./assets/screen.css
    ./assets/screen.css.gz
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails app that is currently an affiliate site with my customer's
I currently have a rails app and each user has a profile that has
Background Currently, I am working on a Rails application. I have different products that
Currently I have a Rails 2.3 app that is a CMS serving over 100
I have a ruby on rails app that has a signup page. Different pages
Currently I have a Rails 3 app that subscribes new users up to MailChimp.
I have a rails 3 app that is currently using Devise for authentication. I
I have a private Rails app that I'm trying to install locally. It's currently
I have Rails 3.1 app that I'm currently hosting on Heroku. My problem is
I'm currently writing a Rails app, and hit a somewhat strange quirk. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.