简体   繁体   中英

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. I created a file called style.css and placed it in /project/public. In my view file (index.html.erb) I added the following line of code:

<%= stylesheet_link_tag "style" %>

When I run rails server I get the following error:

    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 . You have to put the .css file in one of these locations.

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.

This message means you haven't included your file in corresponding folder to interact with assets pipeline (/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 .

then:

Call it with stylesheet_link_tag .

Read more about assets pipeline search-path 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.

as written:

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass and ERB. It allows assets in your application to be automatically combined with assets from other gems.

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

What worked:

  1. I copied the contents of style.css and saved them as style.css.scss

  2. I saved the new file in /project/app/assets/stylesheets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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