【问题标题】:rendering only controller-specific stylesheets not working on first request仅渲染特定于控制器的样式表不适用于第一次请求
【发布时间】:2016-08-29 19:46:14
【问题描述】:

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

您还可以选择仅在各自的控制器中包含特定于控制器的样式表和 JavaScript 文件,方法如下:

%= javascript_include_tag 参数[:controller] %> 或

(由于 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],并且总是可以正常工作。导航栏的样式总是正确的。 问题是,当我访问特定页面时,在 first 页面请求中,没有加载与该控制器相关的 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 += ......

【问题讨论】:

  • 从您的项目中完全删除 turbolinks。它有 90% 的机会解决您的问题。
  • 是否只涉及移除 turbolinks gem?
  • 它解决了我的问题,米哈尔!!!!您应该改写您的评论并将其输入为答案,这样我就可以投票并确认它是我问题的答案。

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


【解决方案1】:

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

  • 移除 turbolinks 宝石
  • 从 application.js 和 application.css 中删除需求
  • 从您的布局中移除 data-turbolinks-track 属性

并考虑在您的下一个项目中使用rails new --skip-turbolinks

【讨论】:

    猜你喜欢
    • 2021-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多