简体   繁体   English

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

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

I'm having a problem with my Jquery and Javascript. 我的Jquery和Javascript出现问题。 I've been building an application, using github to work on it between work and at home. 我一直在构建一个应用程序,使用github在工作和家庭之间进行处理。 At work, everything works fine, all of my Jquery works. 在工作中,一切正常,我的所有Jquery工作正常。 When I pull the app at home, the jquery and javascript does not respond. 当我在家拉应用程序时,jquery和javascript无法响应。 This includes the javascript I'm using from bootstrap. 这包括我从引导程序使用的javascript。

The browsers' javascript console is showing some errors that seem to be the root of the problem but I'm not sure how to go about fixing them. 浏览器的javascript控制台显示了一些错误,这些错误似乎是问题的根源,但我不确定如何解决这些错误。

My application layout include this: 我的应用程序布局包括:

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

and my application.js has this: 而我的application.js具有:

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

What else should I check or try to change? 我还应该检查或尝试更改什么?

My browser javascript console throws some errors as well: 我的浏览器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 is one of my models. 请求是我的模型之一。 Depending on which mvc I'm currently working with, it will search for the resources in those folders. 根据我当前使用的mvc,它将在那些文件夹中搜索资源。 For example when I try to use the javascript that's in my "ecn" view, I get the following errors: 例如,当我尝试使用“ 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

Why would rails be trying to look for the assets in any folder other than the assets folder? 为什么Rails会尝试在资产文件夹以外的任何文件夹中寻找资产?

First you should remove the line <script src="/js/bootstrap.min.js"></script> in your application layout because your application.js already include bootstrap.min.js ( exactly this line : //= require bootstrap.min.js ), so don't need to include it twice, the same thing for this line <link href="css/bootstrap.min.css" rel="stylesheet" media="screen"> you should remove it. 首先,您应该在应用程序布局中删除<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">它。

Second thing is errors you are getting from console : 第二件事是您从控制台获取的错误:

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

it seem that your app try to load bootstrap.min.js and bootstrap.min.css from a folder called requests/js and requests/css , have you already such folders ? 看来您的应用程序尝试从名为requests/jsrequests/css的文件夹中加载bootstrap.min.jsbootstrap.min.css ,您已经这样的文件夹了吗? i think bootstrap generator will use the /app/assets directory if you are using Rails >= 3 exactly app/assets/javascripts/ and app/assets/stylesheets/ 我认为如果您使用Rails> = 3的话,bootstrap generator将使用/app/assets目录,正好是app/assets/javascripts/app/assets/stylesheets/

I think this railscast Twitter Bootstrap Basics can help you too 我认为这个railscast Twitter Bootstrap Basics也可以为您提供帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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