资产管道-未编译/加载的application.js [英] Asset pipeline - application.js not compiled/loaded

查看:81
本文介绍了资产管道-未编译/加载的application.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Rails 4与Foundation 5配合使用,但无法使JavaScript正常工作. Foundation的导航栏菜单未在移动设备中下拉,我遇到问题

I'm using rails 4 with foundation 5 and I'm having trouble getting javascript to work. Foundation's navbar menu doesn't drop down in mobile and I'm having trouble getting the jQuery datepicker to work. After a bit of searching on this site and on Google, I thought the problem might be that I was using sprockets version 2.1.2. However, after upgrading (now at version 3.3.4) the problem still hasn't been solved.

我删除了application.js中的所有内容,并添加了一个函数,可以简单地将一个div追加到另一个div来检查文件是否正在加载,因此application.js现在看起来像这样:

I removed everthing in application.js and added a function to simple append one div to another to check if the file is being loaded so application.js now looks like this:

//= require jquery
//= require jquery_ujs
//= require jquery-ui/datepicker
//= require foundation
//= require_tree .

$(function(){ $(document).foundation(); });

$(document).ready(function (){
  $('#test').append("<div class='test'>Test</div>");
});

不幸的是,div没有被附加.我对javascript很陌生,我不确定还能做什么来弄清楚为什么没有javascript加载的原因.任何帮助,将不胜感激.

Unfortunately, the div does not get appended. I'm pretty new to javascript and I'm not sure what else I can do to figure out why no javascript is being loaded. Any help with this would be much appreciated.

更多信息

当我尝试导航到http://localhost:3000/assets/application.js时,我得到以下信息:

When I try to navigate to http://localhost:3000/assets/application.js I get the following:

抛出错误("Sprockets :: FileNotFound:找不到文件'self'与 输入"application/javascript" \ n(在 /home/sheeka/Documents/workspace/my_projects/ruby/technical_tests/homestay/homestay_test/app/assets/javascripts/application.js:17))

throw Error("Sprockets::FileNotFound: couldn't find file 'self' with type 'application/javascript'\n (in /home/sheeka/Documents/workspace/my_projects/ruby/technical_tests/homestay/homestay_test/app/assets/javascripts/application.js:17)")

我以类似的方式提出application.css.scss没问题,但是我不确定这意味着什么.

I've no problem bringing up application.css.scss in a similar manner but I'm not entirely sure what this means.

根据要求: Github存储库

推荐答案

http ://guides.rubyonrails.org/asset_pipeline.html

您需要在application.js文件中包含require_self.
没有它,该文件中的功能将无法加载.

You need to include a require_self in your application.js file.
Without it the functions in this file will not be loaded.

更新

  1. 在您的app/assets/javascripts/application.js中,将//= require self更改为//= require_self

您的$(document).ready函数未加载,因为 turbolinks gem .
在您的应用程序布局中,更改以下行:
<%= javascript_include_tag "application" 'data-turbolinks-track' => true %>

<%= javascript_include_tag "application" %>

Your $(document).ready functions are not being loaded because the turbolinks gem.
In your application layout change this line:
<%= javascript_include_tag "application" 'data-turbolinks-track' => true %>
To
<%= javascript_include_tag "application" %>

但是,如果您想保留它,请查看以下内容: https://github.com/kossnocorp/jquery.turbolinks

But if you want to keep it, take a look in this gem: https://github.com/kossnocorp/jquery.turbolinks

在使用Turbolink时,您需要了解一些区别,因此请查看他的文档.

There are a few differences that you need to know when you are working with turbolinks, so please take a look in his docs.

这篇关于资产管道-未编译/加载的application.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆