仅渲染特定于控制器的样式表,而不是第一次请求

时间:2016-08-29 19:46:15

标签: ruby-on-rails ruby ruby-on-rails-4 asset-pipeline

在我的应用程序中,我有很多控制器和大量的css,如果把它们放在一起可能会发生冲突。因此,我在控制器到控制器的基础上链接我的样式表。 Ruby on Rails指南建议如下:

  

您还可以选择仅使用以下内容在各自的控制器中包含控制器特定的样式表和JavaScript文件:

     

%= javascript_include_tag params [:controller]%>或<%= stylesheet_link_tag   params [:controller]%>

(我不得不省略第一个< from<%=因为Stack Overflow块报价。)

因此,这是我的application.html.erb文件的结果头:

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag params[:controller] %>
<%= stylesheet_link_tag params[:controller] %>

我已经共享了一个导航栏的css,它在我的application.css文件中是@import [ed],并且总能正常工作。导航栏始终正确设置样式。 问题是,当我访问特定页面时,在第一个页面请求中,未加载到该控制器的相关css。在日志中,永远不会对控制器的样式表发出请求。但是在第二次页面请求中,它会被设置样式,并且日志显示样式表已被请求。

这是为什么?

以下是此问题的一些相关代码:

/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the top of the
 * compiled file, but it's generally better to create a new file per style scope.
 *
 *= require_self
 */
@import 'normalize';
@import 'sass_vars';
@import 'sitewide';
@import "https://fonts.googleapis.com/css?family=Nunito:300,400,700";

在我的config / initializers / assets.rb中,我将我的javascript和css文件的所有名称添加到该行

Rails.application.config.assets.precompile += ......

1 个答案:

答案 0 :(得分:1)

如果您不使用Turbolinks,请将其从项目中删除。它包括:

  • 删除turbolinks gem
  • 从application.js和application.css
  • 中删除
  • 从布局中删除data-turbolinks-track属性

并考虑将rails new --skip-turbolinks用于您的下一个项目。

相关问题