简体   繁体   中英

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

I'm having a problem with my Jquery and Javascript. I've been building an application, using github to work on it between work and at home. At work, everything works fine, all of my Jquery works. When I pull the app at home, the jquery and javascript does not respond. This includes the javascript I'm using from bootstrap.

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.

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:

//= 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:

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. For example when I try to use the javascript that's in my "ecn" view, I get the following errors:

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?

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.

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 ? i think bootstrap generator will use the /app/assets directory if you are using Rails >= 3 exactly app/assets/javascripts/ and app/assets/stylesheets/

I think this railscast Twitter Bootstrap Basics can help you too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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