javascript GET http://localhost:3000/projects/assets/jquery.masonry.js 404(未找到)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14058978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:36:12  来源:igfitidea点击:

GET http://localhost:3000/projects/assets/jquery.masonry.js 404 (Not Found)

javascriptruby-on-rails-3.2asset-pipeline

提问by Daniel Reiner

I was trying to add a javascript-plugin masonry to my rails app and was wondering why it would only work for my welcome controller (localhost:3000/) but not for other controllers as localhost:3000/projects/1, although I'm using exactly the identical code.

我试图向我的 Rails 应用程序添加一个 javascript 插件砌体,并想知道为什么它只适用于我的欢迎控制器 (localhost:3000/) 而不适用于其他控制器,如 localhost:3000/projects/1,虽然我是使用完全相同的代码。

I get this error:

我收到此错误:

GET localhost:3000/projects/assets/jquery.masonry.js 404 (Not Found)

GET localhost:3000/projects/assets/jquery.masonry.js 404(未找到)

and

Uncaught TypeError: Object [object Object] has no method 'masonry'

(anonymous function)

e.resolveWith jquery.min.js

e.extend.ready jquery.min.js

c.addEventListener.z

未捕获的类型错误:对象 [object Object] 没有方法 'masonry'

(匿名函数)

e.resolveWith jquery.min.js

e.extend.ready jquery.min.js

c.addEventListener.z

I asume I get the error because of the wrong path localhost/projects/assets instead of localhost/assets, but I couldn't find out how to fix this. Any help would be welcome!

我假设我因为 localhost/projects/assets 而不是 localhost/assets 的路径错误而收到错误,但我不知道如何解决这个问题。欢迎任何帮助!

This is the code in the view:

这是视图中的代码:

<% content_for(:scripts) do %>
<script type="text/javascript">

  $(function(){
    $('.squarescontainer').masonry({
      // options
      itemSelector : '.item',
      isAnimated: true
    });
  });

</script>
<% end %>

and this is my application.rb:

这是我的 application.rb:

<!DOCTYPE html>
<html>
  <head>
    <title>ifub</title>
    <%= stylesheet_link_tag    "application", :media => "all" %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>
    <link type="text/css" rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans+Narrow&v1">
    <link type="text/css" rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald">

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

    <script src = "assets/jquery.masonry.js"></script>
    <script src = "assets/jquery.masonry.min.js"></script>

    <%= yield(:head) %>

  </head>

  <body>
    <%= render 'layouts/logo' %>

    <p class="notice"><%= notice %></p>
      <p class="alert"><%= alert %></p>

    <div class="frame">
      <div class="container">
        <%= yield %>
        <%= yield :scripts %>
      </div>
    </div>

  </body>
</html>

Thanks for your help!

谢谢你的帮助!

回答by Viktor S.

You are using URL relative to current path, so if your page is localhost:3000/project/page.html, JS url will be localhost:3000/project/assets/.... Try to change your code like this:

您使用的是相对于当前路径的 URL,因此如果您的页面是 localhost:3000/project/page.html,则 JS url 将是localhost:3000/project/assets/.... 尝试像这样更改您的代码:

<script src = "/assets/jquery.masonry.js"></script>
<script src = "/assets/jquery.masonry.min.js"></script>

see "/" added before assets. In that case browser will load script relatively to site root. In that case even if your current page is localhost:3000/project/page.html, JS URL still will be localhost:3000/assets/...

请参阅资产前添加的“/”。在这种情况下,浏览器将加载相对于站点根目录的脚本。在这种情况下,即使您当前的页面是localhost:3000/project/page.html,JS URL 仍然会是localhost:3000/assets/...

Besides, you should connect only one version of plugin - minimized or not

此外,您应该只连接一个版本的插件 - 最小化与否