Search code examples
ruby-on-rails-3.1coffeescript

rails 3.1, why my coffee scripts not working?


I am trying to test coffee script in my rails 3.1 project, but it doesn't get executed, or generated as javascript in the application.js

I have a controller Page, here is the pages.js.coffee:

test = (input) ->
  alert input

test 'hello'

but that alert never get executed at http://127.0.0.1:3000/pages, in fact, the application.js is just empty!

here is the GemFile in case it would help:

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

EDIT

I have added those gems also and installed them, but, the coffee script is not executed

gem 'therubyracer'


gem 'coffee-script-source'
gem 'json'
gem 'execjs'

Solution

  • First: Having "coffee-rails" in your :assets group should be enough. Older versions had "coffee-script" in the :assets group.

    Second: Where are you keeping your *.js.coffee files? You need to make sure they are in an "assets" folder some place. Start with app/assets/javascripts. If they aren't in your assets folder, they won't make it into the asset pipeline.

    If you are doing that already, consider starting a new project and use the default configuration. It should work without any trouble. Compare the working project against the non-working project.

    If all else fails, is it possible for you to share your project with us on GitHub?