alexlod
alexlod

Reputation: 1365

Rails 3.1 Subdomain-specific Assets

I have a setup where I have an engine (non-namespaced) for each subdomain. This works great because each subdomain is able to specify their own custom routes, controllers, views, and models. But I haven't been able to figure out how to get assets to do what I want them to. Ideally javascript and stylesheets are only loaded for a particular subdomain if that subdomain is visited. I don't want one stylesheet definition overwriting another in a different community.

Furthermore, I'd like images to be accessible as normal when using image_tag.

Upvotes: 2

Views: 1193

Answers (1)

alexlod
alexlod

Reputation: 1365

I've figured out a solution. The JS, CSS, and images should all be stored in the default location of the engine, e.g. app/assets/javascripts/<subdomain>/application.js. Then, in the main app's application.html.erb JS and CSS should be included like so:

<%= stylesheet_link_tag "#{community.subdomain}/application" %>
<%= javascript_include_tag "#{community.subdomain}/application" %>

As for images, as long as they're in app/assets/images/<subdomain>/ of the engine, they'll show up in public as /assets/images/<subdomain>/my_image.png. Again, I'm using Rails 3.1; I'm not sure how this behavior is for other versions of Rails, including 3.0.

Upvotes: 2

Related Questions