0

Possible Duplicate:
Rails 3.1 asset pipeline: how to load controller-specific scripts?

Upon creating a new controller and associated view, Rails produces some empty view-specific assets. Take the following command:

rails generate controller home index

This will generate home.css.scss and home.js.coffee. Being new to Rails, my initial thought was that these would be auto-configured to only render in their respective views. It turns out that this is not the case. Instead, the default is that all scripts are rendered in all views. Is this expected? What's the best way to make these assets view-specific?

Community
  • 1
  • 1
David Jones
  • 10,117
  • 28
  • 91
  • 139
  • `home` is a controller, not a view, so `home.css.scss` is related to the whole controller. If you meant 'controller-specific assets', this post is helpful: [Rails 3.1 asset pipeline: how to load controller-specific scripts?](http://stackoverflow.com/questions/6571753/rails-3-1-asset-pipeline-how-to-load-controller-specific-scripts) – Pavel Strakhov Nov 18 '12 at 08:56

1 Answers1

1

The basic setup is that (in production) a single js file is served (and a single CSS file). This is usually more efficient from a caching point of view, at the expense possibly of a slightly slower first page load.

When this gets too much you can create additional manifest files (application.js being the default one) and load them separately. For example one of the apps I work on has a separate admin.js/css since that has so little overlap with the consumer facing portion. You could in theory treat each js/CSS file as a manifest file although you'd need to be calling javascript_include_tag on each page.

If the underlying reason for your question is that you have behaviour you want to attach only on certain pages then either make your selectors/js specific enough that they can tolerate being loaded on all pages, or (I usually do this) add a controller or action specific class on the body and use that.

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174