Rails 4 x SASS: load specific stylesheet in Intern

2019-09-12 16:47发布

In my Rails 4 app, I am trying to implement a conditional to load different stylesheets for Internet Explorer VS. other browsers.

I have an app/views/layout/_header.html.erb partial with:

<head>
  <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
  <!--[if !IE]><!-->
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= stylesheet_link_tag 'ie', media: 'all', 'data-turbolinks-track' => true %>
  <!--<![endif]-->
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>

And I have the following stylesheets in app/assets/stylesheets:

custom / # with all my model-specific stylesheets here, such as posts.scss
application.scss
custom.scss
ie.scss

In application.scss I have:

@import "bootstrap-sprockets";
@import "bootstrap";
@import "bootstrap/theme";
@import "bootstrap-datetimepicker";
@import "font-awesome-sprockets";
@import "font-awesome";
@import "simple_calendar";
@import "custom.scss";
@import "custom/**/*";

Note: the IE-specific rules in ie.scss are very light, less than 20 lines of code.

However, when I launch the app in Internet Explorer, the CSS rules from ie.scss are not taken into account.

How can I make the ie.scss file load when the user launches the app in Internet Explorer?

1条回答
Anthone
2楼-- · 2019-09-12 17:35

For all IE versions:

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<!-- Calling application stylesheet file only once -->
<!--[if IE]> 
  <%= stylesheet_link_tag 'ie', media: 'all', 'data-turbolinks-track' => true %> 
<![endif]-->

And instead of specifying ie.css, you can change it to:

Rails.application.config.assets.precompile += %w( *.css )

So that you don't have to add each and every file separately. What I generally use in my projects:

Rails.application.config.assets.precompile += %w( *.js *.css *.png *.jpg *.jpeg )
查看更多
登录 后发表回答