2011-11-05 58 views
1

我想在我的Rails 3.1的項目,以測試腳本的咖啡,但它並沒有得到執行,或在application.js中rails 3.1,爲什麼我的咖啡腳本不工作?

我有一個控制器Page如JavaScript生成的,這裏是pages.js.coffee :

test = (input) -> 
    alert input 

test 'hello' 

但警報永遠不會在http://127.0.0.1:3000/pages, 實際上執行的是application.js中只是空!

這裏是Gemfile中的情況下,它會幫助:

source 'http://rubygems.org' 

gem 'rails', '3.1.0' 

# Bundle edge Rails instead: 
# gem 'rails',  :git => 'git://github.com/rails/rails.git' 

gem 'sqlite3' 

gem 'devise', '1.4.8' 

gem 'will_paginate', '~> 3.0.2' 

#gem 'nested_form', :git => 'git://github.com/fxposter/nested_form.git' 

gem "simple_form" 

gem 'kaminari' 

gem 'foreigner' 

gem "cocoon" 

# Gems used only for assets and not required 
# in production environments by default. 
group :assets do 
    gem 'sass-rails', " ~> 3.1.0" 
    gem 'coffee-rails', "~> 3.1.0" 
    gem 'uglifier' 
end 

gem "jquery-rails" 

gem 'coffee-script' 
gem 'haml' 
# Use unicorn as the web server 
# gem 'unicorn' 

# Deploy with Capistrano 
# gem 'capistrano' 

# To use debugger 
# gem 'ruby-debug19', :require => 'ruby-debug' 

group :test do 
    # Pretty printed test output 
    gem 'turn', :require => false 
end 

編輯

我也加入了這些寶石和安裝的話,但是,咖啡腳本不執行

​​

回答

3

第一:在您的資產組中擁有「咖啡欄」應該足夠了。資產組中的舊版本具有「coffee-script」。

第二:你在哪裏保留*.js.coffee文件?您需要確保它們位於某個地方的「資產」文件夾中。從app/assets/javascripts開始。如果它們不在您的資產文件夾中,則它們將不會進入資產管道。

如果您已經這樣做了,請考慮啓動一個新項目並使用默認配置。它應該沒有任何麻煩。將工作項目與非工作項目進行比較。

如果一切都失敗了,是否有可能在GitHub上與我們分享您的項目?

+2

感謝您的好主意!我創建了一個新項目,並在我的application.js中注意到了不同點,即「// = require_tree」。簡直是失蹤! – simo

+0

^^這對我來說也是如此。在轉換爲3.1之後,忘記將這些要求添加到application.js的頂部 –

0

請檢查你的config/application.rb中,你應該找到這樣一段:

if defined?(Bundler) 
    # If you precompile assets before deploying to production, use this line 
Bundler.require(*Rails.groups(:assets => %w(development test))) 
# If you want your assets lazily compiled in production, use this line 
#Bundler.require(:default, :assets, Rails.env) 

我懷疑你是不是預編譯上部署的資產,即使你有它設置爲忽略資產寶石。