簡體   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