John Bugner
John Bugner

Reputation: 103

How to make Rails not import every file from app/assets/stylesheets?

In app/assets/stylesheets, I have many sass files. In a requested page, every file in app/assets/stylesheets is imported (with a tag). How do I make it so that not every file from the directory is imported, but only the ones that I pick?

Note: I'm running Rails 4.1.2

Upvotes: 0

Views: 199

Answers (2)

Ruby Racer
Ruby Racer

Reputation: 5740

You change your app/assets/stylesheets/application.css file.

Instead of *=require_tree .

Add:

*=require './file1' 
*=require './file2'
...

You don't need to supply .css or .css.scss

Upvotes: 5

Richard Peck
Richard Peck

Reputation: 76774

Manifest

As alluded to by @Ruby Racer, you'll be looking to manipulate the manifest of your stylesheets in your application:

Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain directives - instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file.

The way to use this is at the top of any of your css files, typically your application.css file -

/app/assets/stylesheets/application.css
/* = require "file" */

By doing this, you'll be able to build your stylesheet assets as you require

--

SASS

If it's only the stylesheets you want to change, you need to remember something else -

If you changed the extension of your CSS to .css.scss, you'll be able to call the @import function of SASS to create a similar effect to the manifest functionality:

#app/assets/stylesheets/application.css.scss
@import "file"

Upvotes: 0

Related Questions