Controller specific stylesheets in rails 3: Inheri

2019-02-02 04:46发布

问题:

Firstly,I'm a newbie to rails.I'm working on a rails website. It has three controllers namely application_controller,static_pages_controllers and users_controller. They all have their respective css (scss) files in app/assets/stylesheets/ (application.css and users.css.scss) except static_pages_controllers and also has a custom.css.scss for overall layout or common elements.I used controller specific stylesheets as mentioned here

My questions are:

1) does the css rules in custom.css apply to all the controllers views except for those I have defined explicitly in seperate controller css?

2) if yes,then I have a rule defined in both custom.css.scss and users.css.scss

custom.css.scss - body { background-color:color1;}

users.css.scss - body { background-color:color2;}

but still views in both static_pages_controllers and users_controllers show background color2. where am I going wrong? only views in users_controller must show color2 and static_pages_controller must show color1.

回答1:

The Rails guide on how to use the asset pipeline doesn't quite tell the whole truth here. It says:

You should put any JavaScript or CSS unique to a controller inside their respective asset files, as these files can then be loaded just for these controllers with lines such as <%= javascript_include_tag params[:controller] %> or <%= stylesheet_link_tag params[:controller] %>.

Now, you could do as they suggest and load specific stylesheets for each controller, but it does not work as they suggest out of the box. The neglect to mention a few things you must do.

  1. You need to remove the //= require_tree . directive from application.css, which, left in place, will load every other asset in the folder. This means that every page would load users.css, and if you added the controller-specific stylesheet line as in their example, it would load the controller stylesheet twice.

  2. You would need to tell Rails to precompile the individual files. By default, all *.css files besides application.css are ignored by the precompiler. To fix this you'd have to do edit your config to do something like this:

    # in environments/production.rb
    # either render all individual css files:
    config.assets.precompile << "*.css"
    # or include them individually
    config.assets.precompile += %w( users.css static_pages.css )
    
  3. Finally, as instructed by the Rails guide, you'd need to change your stylesheet includes to look something like:

    <%# this would now only load application.css, not the whole tree %>
    <%= stylesheet_link_tag :application, :media => "all" %>
    
    <%# and this would load the controller specific file %>
    <%= stylesheet_link_tag params[:controller] %>
    

However, the above may not be truly the best practice. Sure, sometimes you might want individual stylesheets, but most the time you probably just want to serve your style bundle so the client can cache one file. This is how the asset pipeline works out of the box, after all.

Besides that, if you were to just add override rules in your controller specific stylesheets, then you're creating a load-order-specific tangle of styles right out of the gate. This... is probably not good.

A better approach might be to namespace the styles in the controller sheets, something like this:

// in application.css (or some other commonly loaded file)
background-color: $color1;

// in users.css.scss
body.controller-users {
  background-color: $color2;
}

// and so on...

Then in your layout, add the controller name to the body class, like:

<body class="controller-<%= params[:controller] %>">

In this way, your styles are resolved by namespace, not just load order. Furthermore with this solution you could still go ahead and load separate controller-specific stylesheets if you desire, or you could forget about that and just let everything be compiled into application.css as it would be by default. All the styles would be loaded for each page, but only the controller-specific styles would apply.



回答2:

In Rails 4.x

you have to add these lines in config/environment.rb

config.assets.precompile << "*.css"