Rails: How do you map a stylesheet to a layout?

My application has a controller that uses a different layout called "special":

class SessionsController < ApplicationController layout "special" ... end 

So, I created a new layouts/special.html.erb :

 <!DOCTYPE html> <html> <head> <title></title> <%= stylesheet_link_tag "special" %> <%= csrf_meta_tags %> </head> <body> <%= yield %> </body> </html> 

I also created a new matching stylesheets/special.css

The problem is that when I try to access a page with a special layout, I get an exception:

Asterisks :: Helpers :: RailsHelper :: AssetPaths :: AssetNotPrecompiledError in sessions # new

 special.css isn't precompiled 

I have already completed bundle exec rake assets:precompile , but this did not bundle exec rake assets:precompile problem. What's wrong? How to associate a stylesheet with a layout in rails?

+5
source share
2 answers

By default, Rails only precompiles your application.css and application.js files (or their equivalents .scss , .less or .coffee ).

If you want the additional file to be precompiled, you must add it to the precompile array at config/environments/production.rb , for example:

 config.assets.precompile += %w( special.css ) 

See: http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

+3
source

A slight change to the accepted answer - the documents suggest adding precompiled resources to config/initializers/assets.rb . This makes sense, so they are available in all environments without having to repeat the settings configured for each. So I created this file and added the full configuration attribute:

 Rails.application.config.assets.precompile += %w( special.css ) 
0
source

Source: https://habr.com/ru/post/1402361/


All Articles