Rails:Jquery/Javascript无法从正确的资产位置加载

Rails: Jquery/Javascript fails to load from correct assets location

本文关键字:加载 位置 Jquery Javascript Rails      更新时间:2023-09-26

我的Jquery和Javascript有问题。我一直在构建一个应用程序,使用github在工作和家庭之间进行工作。在工作中,一切都很好,我所有的Jquery都很好。当我在家里拉应用程序时,jquery和javascript没有响应。这包括我从引导程序中使用的javascript。

浏览器的javascript控制台显示了一些错误,这些错误似乎是问题的根源,但我不知道如何修复它们。

我的应用程序布局包括:

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

我的应用程序.js有这样一个:

//= require jquery
//= require jquery_ujs
//= require_tree .
//= require bootstrap.min.js

我还应该检查或尝试更改什么?

我的浏览器javascript控制台也抛出了一些错误:

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

requests是我的模式之一。根据我目前使用的mvc,它将搜索这些文件夹中的资源。例如,当我尝试使用"ecn"视图中的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

为什么rails会试图在资产文件夹之外的任何文件夹中查找资产?

首先,您应该在应用程序布局中删除<script src="/js/bootstrap.min.js"></script>行,因为您的application.js已经包含bootstrap.min.js(确切地说,这一行是//= require bootstrap.min.js),所以不需要包含两次,对于这一行<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">,您应该删除它。

第二件事是你从控制台得到的错误:

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

你的应用程序似乎试图从一个名为requests/jsrequests/css的文件夹加载bootstrap.min.jsbootstrap.min.css,你已经有这样的文件夹了吗?我认为引导生成器将使用/app/assets目录,如果您使用的Rails>=3恰好是app/assets/javascripts/app/assets/stylesheets/

我认为这个railscast推特引导基础知识也可以帮助你