简体   繁体   English

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

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

I'm trying to add a CSS file to my rails project. 我正在尝试将CS​​S文件添加到我的rails项目中。 I created a file called style.css and placed it in /project/public. 我创建了一个名为style.css的文件,并将其放在/ project / public中。 In my view file (index.html.erb) I added the following line of code: 在我的视图文件(index.html.erb)中,添加了以下代码行:

<%= stylesheet_link_tag "style" %>

When I run rails server I get the following error: 当我运行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.

How can I fix this issue? 如何解决此问题?

You can check more about assets in the guidelines 您可以在准则中查看有关资产的更多信息

Basically, Rails is searching for the assets, inside an application in one of three locations: app/assets , lib/assets or vendor/assets . 基本上,Rails在三个位置之一的应用程序内搜索资产: app / assetslib / assetsvendor / assets You have to put the .css file in one of these locations. 您必须将.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.

The asset "style.css" is not present in the asset pipeline. 资产“ style.css”不在资产管道中。

This message means you haven't included your file in corresponding folder to interact with assets pipeline (/project/public). 此消息表示您尚未将文件包含在与资源管道(/ project / public)进行交互的相应文件夹中。

In order to work with assets pipeline you have to follow a few rules: 为了使用资产管道,您必须遵循一些规则:

first: 第一:

Put it in inteded folder (vendor or assets or lib) this are three different folders in your project folder that assets pipeline is designed to search for files declared in manifest or called individualy with stylesheet_link_tag . 将其放在inteded文件夹中(供应商,资产或lib),这是项目文件夹中的三个不同文件夹,资产管道旨在搜索清单中声明的​​文件或使用stylesheet_link_tag单独命名的文件。

then: 然后:

Call it with stylesheet_link_tag . stylesheet_link_tag调用它。

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

side note: 边注:

you could also use the application manifest which rails provides assets/application.css and assets/application.js and call it in your html instead of calling each file seperatly .. which is kind of the intention of assets pipeline. 您还可以使用Rails提供assets/application.cssassets/application.js的应用程序清单,并在html中调用它,而不是分别调用每个文件..这是资产管道的目的。

as written: 如所写:

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. 资产管道提供了一个框架,用于连接和最小化或压缩JavaScript和CSS资产。 It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass and ERB. 它还增加了用其他语言和预处理器(例如CoffeeScript,Sass和ERB)编写这些资产的功能。 It allows assets in your application to be automatically combined with assets from other gems. 它允许您的应用程序中的资产与其他gem的资产自动合并。

you are missing all this builtin goods when not declaring you files in manifest. 如果未在清单中声明文件,则将丢失所有这些内置商品。

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

What worked: 什么有效:

  1. I copied the contents of style.css and saved them as style.css.scss 我复制了style.css的内容,并将其另存为style.css.scss

  2. I saved the new file in /project/app/assets/stylesheets 我将新文件保存在/ project / app / assets / stylesheets中

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

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