Search code examples
ruby-on-railsrubybackbone.jssyntaxcoffeescript

Rails coffeescript + backbone syntax error


i got following router code:

class App.Routers.MainRouter extends Backbone.Router
  routes:
    '': 'index'
  index: ->
    console.log "whatever"

And got following error from Rails

ActionView::Template::Error ([stdin]:1:84: error: unexpected :
    console.log "whatever"er extends Backbone.Router
                                                                                   ^
  (in /Users/ivan/projects/ruby/backbone-example/app/assets/javascripts/backbone/routers/main_router.js.coffee)):
    15:     = favicon_link_tag 'apple-touch-icon-72x72-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png', :sizes => '72x72'
    16:     = favicon_link_tag 'apple-touch-icon-precomposed.png', :rel => 'apple-touch-icon-precomposed', :type => 'image/png'
    17:     = favicon_link_tag 'favicon.ico', :rel => 'shortcut icon'
    18:     = javascript_include_tag "application"
    19:
    20:
    21:   %body
  app/views/layouts/application.html.haml:18:in `_app_views_layouts_application_html_haml___4072892406181986467_2205219860'

execjs (2.2.1) lib/execjs/ruby_racer_runtime.rb:47:in `rescue in block in call'
execjs (2.2.1) lib/execjs/ruby_racer_runtime.rb:43:in `block in call'
execjs (2.2.1) lib/execjs/ruby_racer_runtime.rb:78:in `block in lock'
execjs (2.2.1) lib/execjs/ruby_racer_runtime.rb:76:in `call'
execjs (2.2.1) lib/execjs/ruby_racer_runtime.rb:76:in `Locker'
execjs (2.2.1) lib/execjs/ruby_racer_runtime.rb:76:in `lock'
execjs (2.2.1) lib/execjs/ruby_racer_runtime.rb:42:in `call'
coffee-script (2.2.0) lib/coffee_script.rb:57:in `compile'

Can't wrap my head around what is wrong?

therubyracer (0.12.1)
libv8 (~> 3.16.14.0)

Gemfile:

source 'https://rubygems.org'
gem 'rails', '4.1.4'
gem 'pg'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'therubyracer',  platforms: :ruby
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0',          group: :doc
gem 'spring',        group: :development
gem 'rails-backbone', github: 'codebrew/backbone-rails', branch: 'master'
gem 'twitter-bootstrap-rails'
gem 'haml-rails'

application.js

//= require jquery
//= require twitter/bootstrap
//= require underscore
//= require backbone
//= require backbone/backbone_example

backbone_example.js.coffee

#= require_self
#= require_tree ./templates
#= require_tree ./models
#= require_tree ./views
#= require_tree ./routers

    window.App =
      Models: {}
      Collections: {}
      Routers: {}
      Views: {}
      initialize: ->
        new App.Routers.MainRouter()
        Backbone.history.start()

if i remove this two lines routes: '': 'index' coffeescript compiles successfully, so the problem is there. Another strange thing, if i copy n paste this file to the js2coffee.org it's compiling it successfully too.


Solution

  • You might have some stray carriage-return (CR - \r) characters in your code, or some other ASCII character that your editor doesn't show you.