问题描述
我正在将Rails 4与Foundation 5配合使用,但无法使JavaScript正常工作. Foundation的导航栏菜单未在移动设备中下拉,我遇到问题.在该站点和Google上进行了一些搜索之后,我认为问题可能出在我使用链轮版本2.1.2.但是,升级后(现在是3.3.4版),问题仍然没有解决.
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:
我以类似的方式提出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.
更新
-
在您的
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!