新的Rails 3.2.8应用程序未加载jQuery [英] New Rails 3.2.8 app not loading jQuery

查看:74
本文介绍了新的Rails 3.2.8应用程序未加载jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天做了rails new,然后创建了一个简单的搜索栏:

I did a rails new today, then created a simple search bar:

<form>
  <input id='search-bar' type='search' name='search'></input>
</form>
<a id='search-btn' href="#">searching!</a>

好的,一些点击处理程序呢!因此,assets/javascripts/searches.js.coffee:

Alright, how about some click handlers! So assets/javascripts/searches.js.coffee:

$('#search-btn').click ->
  alert 'BOOM'
  query = $('#search-bar').html
  console.log query

而且...什么都没有.因此,我进入了Chrome控制台:

And... nothing. So I go to the Chrome console:

>$('a')
null
>$('body')
null
>$
function () { [native code] }

没有选择器的jQuery,不太有用.这里发生了什么?如果我在页面或layout.html.erb中添加<script type="text/javascript" src='http://code.jquery.com/jquery-1.8.2.js'></script>,我们都很好.

a jQuery with no selectors, not too helpful. What is going on here? If I add <script type="text/javascript" src='http://code.jquery.com/jquery-1.8.2.js'></script> to the page or my layout.html.erb, we're all good.

我遇到了同样的问题,尽管在资产/javascript中,underscore-min.js(注意不是.min.js)无法加载.给定我的(未修改的)application.js,它应该加载当前文件夹:

I'm having the same issue with underscore-min.js (note it is not .min.js) not loading despite being in assets/javascripts. Given my (unmodified) application.js, it should load the current folder:

//= require jquery
//= require jquery_ujs
//= require_tree .

更新

如果我转到Chrome检查器的资源"标签并签出application.js,则会看到以下信息:

If I go to the Chrome inspector's Resources tab and check out application.js, I see this:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
;

在目录中似乎没有正确包含js文件

That doesn't look like it is properly including the js files inside its directory

推荐答案

由于某些原因,链轮无法正常工作.检查您是否正确包含了所有与资产相关的gem,并且您没有更改config/environments/development.rb.显然,请确保在public/javascripts/中没有application.js,应该在app/assets/javascripts中.

Sprockets aren't working for some reason. Check that you are properly including all the assets-related gems and that you didn't change config/environments/development.rb. Obviously make sure there isn't application.js in public/javascripts/, it should be in app/assets/javascripts.

这篇关于新的Rails 3.2.8应用程序未加载jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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