简体   繁体   English

Rails试图从错误的位置加载资产

[英]Rails trying to load assets from incorrect locations

I'm having a problem with my javascript and jquery. 我的javascript和jquery有问题。 I'm using bootstrap in addition to some self written scripts. 除了一些自行编写的脚本之外,我还在使用引导程序。 When I attempt a javascript driven action (ie clicking a button that shows a hidden div, or openning a modal), the browser does not respond. 当我尝试执行JavaScript驱动的操作(即单击显示隐藏的div的按钮或打开模式)时,浏览器不响应。 The browser's javascript console shows the following errors: 浏览器的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

Notice that the location is in the folder "ecns" rather than "assets". 请注意,该位置在文件夹“ ecns”中,而不是在“ assets”中。 Ecns is one of my models, the one I currently was working with in my browser, if I go to a different part of my app, the error looks like this: Ecns是我的模型之一,是我当前在浏览器中使用的模型,如果我转到应用程序的其他部分,则错误如下所示:

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

I know there is not a problem with the scripts themselves because they work on different computers. 我知道脚本本身没有问题,因为它们可以在不同的计算机上工作。 I'm assuming I have something in rails either configured or installed incorrectly. 我假设我在滑轨中配置或安装不正确。 For reference, here is my application.js: 供参考,这是我的application.js:

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

And the following is in my application layout: 以下是我的应用程序布局:

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

Why would rails be looking to the incorrect locations for the assets? 为什么Rails会寻找资产的不正确位置? I'm running rails 3.2.1 我正在运行Rails 3.2.1

Rails is using a custom asset pipeline, that will make all the assets visible under the assets subdirectory in the root directory of the app. Rails是使用自定义资产管道,这将使下的所有资产,可见assets在应用程序的根目录下创建子目录。

So if your base address is http://www.something.com/ all assets will live under http://www.something.com/assets/ . 因此,如果您的基本地址为http://www.something.com/所有资产都将位于http://www.something.com/assets/下。

If you want to import a CSS or a JS file, and if you placed them correctly in the app/assets/ directory in Rails, you should use these commands: 如果要导入CSS或JS文件,并且将它们正确放置在Rails的app/assets/目录中,则应使用以下命令:

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

Placing them wherever you need in your layout. 将它们放置在布局中所需的任何位置。

If you still want to import them manually (and I'd not recommend it), use these: 如果您仍然想手动导入它们(我不建议这样做),请使用以下命令:

<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