繁体   English   中英

CSS样式表无法在Ruby on Rails上使用:Sprockets :: Rails :: Helper :: AssetNotFound错误

[英]CSS Stylesheet not working using Ruby on Rails: Sprockets::Rails::Helper::AssetNotFound Error

我正在尝试将CS​​S文件添加到我的rails项目中。 我创建了一个名为style.css的文件,并将其放在/ project / public中。 在我的视图文件(index.html.erb)中,添加了以下代码行:

<%= stylesheet_link_tag "style" %>

当我运行Rails服务器时,出现以下错误:

    Sprockets::Rails::Helper::AssetNotFound in Articles#index
    Showing /mnt/c/code/blog/app/views/articles/index.html where line #34 

raised:

The asset **"style.css"** is not present in the asset pipeline.

如何解决此问题?

您可以在准则中查看有关资产的更多信息

基本上,Rails在三个位置之一的应用程序内搜索资产: app / assetslib / assetsvendor / assets 您必须将.css文件放在以下位置之一。

app/assets is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.

lib/assets is for your own libraries' code that doesn't really fit into the scope of the application or those libraries which are shared across applications.

vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks. Keep in mind that third party code with references to other files also processed by the asset Pipeline (images, stylesheets, etc.), will need to be rewritten to use helpers like asset_path.

资产“ style.css”不在资产管道中。

此消息表示您尚未将文件包含在与资源管道(/ project / public)进行交互的相应文件夹中。

为了使用资产管道,您必须遵循一些规则:

第一:

将其放在inteded文件夹中(供应商,资产或lib),这是项目文件夹中的三个不同文件夹,资产管道旨在搜索清单中声明的​​文件或使用stylesheet_link_tag单独命名的文件。

然后:

stylesheet_link_tag调用它。

阅读有关资产管道搜索路径的更多信息https://guides.rubyonrails.org/asset_pipeline.html#search-paths

边注:

您还可以使用Rails提供assets/application.cssassets/application.js的应用程序清单,并在html中调用它,而不是分别调用每个文件..这是资产管道的目的。

如所写:

资产管道提供了一个框架,用于连接和最小化或压缩JavaScript和CSS资产。 它还增加了用其他语言和预处理器(例如CoffeeScript,Sass和ERB)编写这些资产的功能。 它允许您的应用程序中的资产与其他gem的资产自动合并。

如果未在清单中声明文件,则将丢失所有这些内置商品。

详细阅读https://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives

什么有效:

  1. 我复制了style.css的内容,并将其另存为style.css.scss

  2. 我将新文件保存在/ project / app / assets / stylesheets中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM