簡體   English   中英

Rails試圖從錯誤的位置加載資產

[英]Rails trying to load assets from incorrect locations

我的javascript和jquery有問題。 除了一些自行編寫的腳本之外,我還在使用引導程序。 當我嘗試執行JavaScript驅動的操作(即單擊顯示隱藏的div的按鈕或打開模式)時,瀏覽器不響應。 瀏覽器的JavaScript控制台顯示以下錯誤:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3000/ecns/css/bootstrap.min.css
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3000/ecns/js/bootstrap.min.js

請注意,該位置在文件夾“ ecns”中,而不是在“ assets”中。 Ecns是我的模型之一,是我當前在瀏覽器中使用的模型,如果我轉到應用程序的其他部分,則錯誤如下所示:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3000/requests/js/bootstrap.min.js

我知道腳本本身沒有問題,因為它們可以在不同的計算機上工作。 我假設我在滑軌中配置或安裝不正確。 供參考,這是我的application.js:

//= require jquery
//= require jquery_ujs
//= require_tree .

以下是我的應用程序布局:

<head>
  <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
  <title>Site</title>
  <%= javascript_include_tag "application" %>
  <%= stylesheet_link_tag    "application", media: "all" %>
  <%= csrf_meta_tags %>
</head>
<body>
...
  <script src="js/bootstrap.min.js"></script>
</body>

為什么Rails會尋找資產的不正確位置? 我正在運行Rails 3.2.1

Rails是使用自定義資產管道,這將使下的所有資產,可見assets在應用程序的根目錄下創建子目錄。

因此,如果您的基本地址為http://www.something.com/所有資產都將位於http://www.something.com/assets/下。

如果要導入CSS或JS文件,並且將它們正確放置在Rails的app/assets/目錄中,則應使用以下命令:

<%= javascript_include_tag "bootstrap.min" %>
<%= stylesheet_link_tag    "bootstrap.min" %>

將它們放置在布局中所需的任何位置。

如果您仍然想手動導入它們(我不建議這樣做),請使用以下命令:

<script src="assets/bootstrap.min.js"></script>
<link href="assets/bootstrap.min.css" rel="stylesheet" media="screen">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM