Application.js.coffee rails [英] Application.js.coffee rails

查看:255
本文介绍了Application.js.coffee rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从jQuery做一个自动完成,但我的代码是在CoffeeScript。我不知道如何在我的本地主机服务器上运行它。



一开始我把它放在application.js,但是不工作,所以我创建了发电



这里是代码:

  jQuery  - > 
$('#querysearch')。autocomplete
source:['foo','food','four']

这里application.js正在调用文件:

  // = require gens 
// = require jQuery.ui.datepicker
#file his gens.js.coffee

这是我如何调用application.js:

 <%= javascript_include_tagapplication%& 

我做错了什么?

解决方案

首先我要确保我的Gemfile中有CoffeeScript和jQuery UI gem。

  group:assets do 
gem'coffee-rails'
gem'jquery-ui-rails'
end

然后我会确保我正确的要求这些资产。我认为你在这里犯错误


  1. 通过大写jquery中的 Q / li>
  2. 通过要求使用datepicker,然后尝试通过在您的gens文件之后要求您的jQuery UI库调用自动完成


它应该像这样

  // = require jquery。 ui.autocomplete 
// = require gens

您可以放置​​JS代码直接进入 application.js 文件,如果你喜欢或它应该在你的 gens.js.coffee 文件中确定。



您也可以尝试运行您的服务器并访问 http:// localhost:3000 / assets / application.js



我也会尝试在Chrome浏览器中打开开发工具,并寻找JavaScript错误。


I am trying to do an autocomplete from jQuery, however the code that I have is in CoffeeScript. I am not to sure how to run it on my localhost server.

At first I placed it in application.js but that doesn't work, so then I created gens.js.coffee and placed the code there, but still doesn't show.

Here is the code:

jQuery ->
  $('#querysearch').autocomplete
    source: ['foo', 'food', 'four']

Here application.js is calling the file:

//= require gens
//= require jQuery.ui.datepicker
# file his actually gens.js.coffee

This is how I call application.js:

<%= javascript_include_tag "application" %>

What I am doing wrong?

解决方案

Firstly I would make sure that I have the CoffeeScript and jQuery UI gems in my Gemfile.

group :assets do
  gem 'coffee-rails'
  gem 'jquery-ui-rails'
end

Then I would make sure I was requiring these assets correctly. I think you are making mistakes here

  1. by capitalizing the Q in jquery.
  2. by requiring datepicker then trying to call autocomplete
  3. by requiring your jQuery UI library after your 'gens' file.

It should look like this

//= require jquery.ui.autocomplete
//= require gens

You can place JS code (NOT CoffeeScript) straight into the application.js file if you like or it should be ok in your gens.js.coffee file.

You could also try running your server and visiting http://localhost:3000/assets/application.js. You can look at the JS there and see if it looks correct.

I would also try to open Dev tools in the Chrome browser and look for JavaScript errors.

这篇关于Application.js.coffee rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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