18

I've created a clean and simple Rails 3.1 application called demo1. It contains two controllers - hello and goodbye. I get the following error when I try to load http://localhost:3000/say/hello:

ActionView::Template::Error (
  (in /home/me/dev/ruby/demo1/app/assets/javascripts/say.js.coffee)):
    3: <head>
    4:   <title>Demo1</title>
    5:   <%= stylesheet_link_tag    "application" %>
    6:   <%= javascript_include_tag "application" %>
    7:   <%= csrf_meta_tags %>
    8: </head>
    9: <body>
  app/views/layouts/application.html.erb:6:in     
`_app_views_layouts_application_html_erb___558576499_89622220'

The problematic line of application.html.erb is:

<%= javascript_include_tag "application" %>

When I created the application using Rails 3.0 this particular line was:

<%= javascript_include_tag :defaults %>

And this worked fine. In fact, when I change the application.html.erb to use :defaults everything works, but I want to use the new features of Rails 3.1.

I can't seem to turn up anything on Google around this, I guess because Rails 3.1 has just been released.

By the way, I'm following the first chapter in the Agile Web Development with Rails (4th edition) Updated for Rails 3.1 book.

Some environmental info that may help in answering this question:

$ cat /etc/issue
Ubuntu 10.04.2 LTS \n \l

$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]

$ rails -v
Rails 3.1.0

Contents of the say.js.coffee file:

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
ryan
  • 5,039
  • 13
  • 35
  • 42
  • Quick question, did everything install correctly when you ran bundle install? I wonder if something is wrong with exec.js – agmcleod Sep 02 '11 at 13:55
  • Yes, I re-ran bundle install again and everything seems fine... any ideas how I could dig deeper? – ryan Sep 02 '11 at 14:01
  • Try looking at the terminal output from when you created the app to see if something failed to install. Ive been using rails 3.1 since beta versions. I tried creating the demo app from scratch as the book suggests, and I created the controller. It worked as expected. Again, i already have execjs and coffee-script installed. So it just had to find it and tie it to the locked gemfile. – agmcleod Sep 02 '11 at 14:06
  • Great suggestion. So did you install execjs and coffee-script prior to using Rails 3.1 or were they installed when creating a project in Rails 3.1? – ryan Sep 02 '11 at 14:33
  • They come in the default gemfile with rails 3.1. Because ive been using the beta versions for months now, ive already had those installed. – agmcleod Sep 02 '11 at 15:02
  • I figured it out - see my answer. – ryan Sep 02 '11 at 15:06

14 Answers14

33

Ok, I've figured out what the issue was, and so I'll answer my own question.

The problem was the app/assets/javascripts/application.js file contained commented out code. However, one of the commented out lines was as follows:

//= require_tree .

When I delete this line everything works fine.

Hope this helps. If someone can provide some insight as to why the underscore was causing the issue that would be great.

ryan
  • 5,039
  • 13
  • 35
  • 42
  • 4
    The line as you put in your answer is actually correct. That will include all javascript files under folders assets/javascripts. See: http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives. A rails 3.1 app i created earlier today has that and it works fine. I also have two empty coffeescript files. – agmcleod Sep 02 '11 at 18:13
  • 2
    I am starting off with ROR and faced the same issue, and the same solution works! However I still dont get from the dicussion if deleting the line is something preferable and how is it solving the problem? – Nitish Upreti Sep 18 '11 at 15:46
  • 4
    Doing this might stop the error occurring, but it isn't really the solution. The 'commented out' line is really a processing directive for the asset pipeline. I'm not sure exactly what your issue was, but perhaps the same JS file was being included twice? – DavidJ Sep 24 '12 at 14:18
  • I have to delete //= require turbolinks also to get the app running – bhanu Oct 12 '15 at 13:39
  • Thanks, it helped. – Mukesh Sep 28 '16 at 09:22
7

I just ran into this issue when starting a new RoR 3.2.1 app. The problem is that you are missing a JS runtime, which is because the line

# gem 'therubyracer'

in your Gemfile has that # in front of it. God only knows why they ship Rails with that line commented, because any tutorial that uses generate scaffold or similar will result in the asset compilation process tripping up over the generated coffeescript file.

The solution is to uncomment that line. Installing another JS runtime may also solve the problem. See e.g. ExecJS and could not find a JavaScript runtime.

The official ruby on rails guide says:

Compiling CoffeeScript to JavaScript requires a JavaScript runtime and the absence of a runtime will give you an execjs error. Usually Mac OS X and Windows come with a JavaScript runtime installed. Rails adds the therubyracer gem to Gemfile in a commented line for new apps and you can uncomment if you need it. therubyrhino is the recommended runtime for JRuby users and is added by default to Gemfile in apps generated under JRuby. You can investigate about all the supported runtimes at ExecJS.

Community
  • 1
  • 1
Confusion
  • 16,256
  • 8
  • 46
  • 71
  • This works and fixes the issue, I was following the intro to rails tutorials on ubuntu 12.04 with rails 3.2 and got this error – Matt Wheeler May 06 '12 at 08:05
3

Simply delete the 2 lines from application.js

//= require turbolinks

//= require_tree

Community
  • 1
  • 1
Talha
  • 1,546
  • 17
  • 15
2

If you're on Windows and you used RailsInstaller to get your development going, you need to use that .bat file included with RailsInstaller to run your server. Also you can't use any CMD hook like conemu to start the server, unfortunately.

Knyteguy
  • 21
  • 1
2

For the benefit of future Googlers, I went down a rabbit hole trying to install therubyracer until I stumbled upon this related post:

ExecJS::RuntimeError in rails 3.2.8 engine with javascript_include_tag

The link mentioned explains that ExecJS is configured incorrectly for Windows 8 out-of-the-box. In particular, the workaround described by pottsk did the trick for me.

https://github.com/sstephenson/execjs/issues/81#issuecomment-9892952

It involves changing the way that the cscript executable is run by ExecJS in %rubyinstall%\gems\2.0.0\gems\execjs-2.0.1\lib\execjs\runtimes.rb:

# JScript = ExternalRuntime.new(
#   :name        => "JScript",
#   :command     => "cscript //E:jscript //Nologo //U",
#   :runner_path => ExecJS.root + "/support/jscript_runner.js",
#   :encoding    => 'UTF-16LE' # CScript with //U returns UTF-16LE
# )

JScript = ExternalRuntime.new(
    :name        => "JScript",
    :command     => "cscript //E:jscript //Nologo",
    :runner_path => ExecJS.root + "/support/jscript_runner.js",
    :encoding    => 'UTF-8' # CScript with //U returns UTF-16LE
)

It goes without saying that this is not a permanent solution but it does get me up and running until the patch is incorporated.

Community
  • 1
  • 1
Cameron Flint
  • 450
  • 5
  • 15
  • yuck but wouldnt this use the same js engine as IE? Better off using therubyrhino or even better NodeJS – j_mcnally Oct 15 '13 at 03:31
2

I had the same problem running rails 3.1.1

Once I reinstated the following code in my Gemfile, my issue disappeared:

group :assets do
  gem 'sass-rails',   '~> 3.1.4'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

This was placed in my Gemfile when initially setting up the project, i.e. using "rails new myapp".

Note that the in my template file I include:

<%= javascript_include_tag :application %>

Hope that helps

DavB
  • 1,676
  • 2
  • 14
  • 16
1

Removing //= require_tree will just stop your javascript files from being loaded into rails. It will probably stop the error, but probably not what you'd like.

However, in my case, I had some .js.coffee files with incorrect indenting. Once I fixed this (by deleting them), it worked.

thedanotto
  • 6,895
  • 5
  • 45
  • 43
0

In case of using linux(Ubuntu linux like mine), install javascript runtime. The best is issue the below command to install,

apt-get install nodejs

or else, for each and every new creation of rails project, add

gem 'therubyracer' to your gemfile and run bundle install

hariharan kumar
  • 439
  • 4
  • 8
0
devise_scope :user do
      get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
   end

If your routes.rb has this above line just delete it and run. This is what corrected my problem.

Gautam
  • 1,754
  • 1
  • 14
  • 22
0

modify generator file (application.html.erb.tt) as the following:

<%= javascript_include_tag "application", "data-turbolinks-track" true %>
<%= javascript_include_tag :default, "data-turbolinks-track" true %>
cmd
  • 11,622
  • 7
  • 51
  • 61
kys48
  • 1
0

Just REMOVE the following line (Line no. 6) from generator file (application.html.erb) :

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

please update me if there is any drawback to remove this line

Thanks

Skyfall
  • 57
  • 8
0

I have this issue in Rails 4, and if I switch it to 'defaults' rather than 'application', it works, just as the OP says. But this is in an external Gem that I'd rather not edit. I have found that if I force my Gemfile to use version 1.8.0 of coffee-script-source, do a bundle install, and start the server, then everything works fine.

metaforge
  • 911
  • 10
  • 11
0

Adding the gem 'therubyracer' fixed the issue. I was getting the error only after adding external js file through <%= javascript_include_tag %>

0

Another thing you can try which worked for me is to add the json (and perhaps also the yajl-ruby) gem to your Gemfile. I don't really understand why they weren't already installed before because:

rails depends on actionpack which depends on sprockets which depends on json and tilt

and tilt depends on yajl-ruby

Yet, somehow, it seems many of the dependent gems were not installed!?

billmccord
  • 98
  • 6