Rails 3 - Precompiling all css, sass and scss files in a folder

前端 未结 2 1174
梦毁少年i
梦毁少年i 2020-12-11 07:35

So I have a project with page-specific styles and css. I\'ve put those files into their own directory assets/#{type}/page-specific/ and I\'m attempting to write

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 07:53

    OK, I think I get it now. Suppose you have these two files:

    app/assets/stylesheets/page-specific/default.css
    app/assets/stylesheets/page-specific/more-specific/home.css.scss
    

    After you add this to your config/initializers/assets.rb

    Rails.application.config.assets.paths << Rails.root.join('app', 'assets', 'stylesheets', 'page-specific')
    Rails.application.config.assets.precompile << /.+\.css$/
    

    Please note that this should work even for directories outside 'app/assets' (of course, you'll need to modify the path that gets added to config.assets.paths).

    You should be able to reference them in your ERB pages like this:

      <%= stylesheet_link_tag 'default', media: 'all' %>
      <%= stylesheet_link_tag 'more-specific/home', media: 'all' %>
    

    I hope this helps.

提交回复
热议问题