Rails ExecJS::ProgramError in Pages#home?

2019-01-01 03:54发布

问题:

Starting a new app, when I create a controller page home and try to go to local host:3000/pages/home, I get the following error:

Showing c:/Users/Doesha/desktop/pinplug/app/views/layouts/application.html.erb where line #6 raised:

TypeError: Object doesn\'t support this property or method
  (in c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/turbolinks-2.5.3/lib/assets/javascripts/turbolinks.js.coffee)

application.html.erb file:

<!DOCTYPE html>
<html>
<head>
  <title>Pinplug</title>
  <%= stylesheet_link_tag    \'application\', media: \'all\', \'data-turbolinks-track\' => true %>
  <%= javascript_include_tag \'application\', \'data-turbolinks-track\' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

application_controller.rb file:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end

pages_controller.rb file:

class PagesController < ApplicationController
  def home
  end
end

routes.rb file:

Rails.application.routes.draw do
  get \'pages/home\'

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with \"rake routes\".

  # You can have the root of your site routed with \"root\"
  # root \'welcome#index\'

  # Example of regular route:
  #   get \'products/:id\' => \'catalog#view\'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get \'products/:id/purchase\' => \'catalog#purchase\', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get \'short\'
  #       post \'toggle\'
  #     end
  #
  #     collection do
  #       get \'sold\'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get \'recent\', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post \'toggle\'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end

gemfile:

source \'https://rubygems.org\'


# Bundle edge Rails instead: gem \'rails\', github: \'rails/rails\'
gem \'rails\', \'4.2.0\'
# Use sqlite3 as the database for Active Record
gem \'sqlite3\'
# Use SCSS for stylesheets
gem \'sass-rails\', \'~> 5.0\'
# Use Uglifier as compressor for JavaScript assets
gem \'uglifier\', \'>= 1.3.0\'
# Use CoffeeScript for .coffee assets and views
gem \'coffee-rails\', \'~> 4.1.0\'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem \'therubyracer\', platforms: :ruby

gem \'execjs\', \'~> 2.2.2\'

# Use jquery as the JavaScript library
gem \'jquery-rails\'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem \'turbolinks\'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem \'jbuilder\', \'~> 2.0\'
# bundle exec rake doc:rails generates the API under doc/api.
gem \'sdoc\', \'~> 0.4.0\', group: :doc

# Use ActiveModel has_secure_password
# gem \'bcrypt\', \'~> 3.1.7\'

# Use Unicorn as the app server
# gem \'unicorn\'

# Use Capistrano for deployment
# gem \'capistrano-rails\', group: :development

group :development, :test do
  # Call \'byebug\' anywhere in the code to stop execution and get a debugger console
  gem \'byebug\'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem \'web-console\', \'~> 2.0\'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem \'tzinfo-data\', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gemfile.lock file:

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.2.0)
      actionpack (= 4.2.0)
      actionview (= 4.2.0)
      activejob (= 4.2.0)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 1.0, >= 1.0.5)
    actionpack (4.2.0)
      actionview (= 4.2.0)
      activesupport (= 4.2.0)
      rack (~> 1.6.0)
      rack-test (~> 0.6.2)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.1)
    actionview (4.2.0)
      activesupport (= 4.2.0)
      builder (~> 3.1)
      erubis (~> 2.7.0)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.1)
    activejob (4.2.0)
      activesupport (= 4.2.0)
      globalid (>= 0.3.0)
    activemodel (4.2.0)
      activesupport (= 4.2.0)
      builder (~> 3.1)
    activerecord (4.2.0)
      activemodel (= 4.2.0)
      activesupport (= 4.2.0)
      arel (~> 6.0)
    activesupport (4.2.0)
      i18n (~> 0.7)
      json (~> 1.7, >= 1.7.7)
      minitest (~> 5.1)
      thread_safe (~> 0.3, >= 0.3.4)
      tzinfo (~> 1.1)
    arel (6.0.0)
    binding_of_caller (0.7.2)
      debug_inspector (>= 0.0.1)
    builder (3.2.2)
    byebug (3.5.1)
      columnize (~> 0.8)
      debugger-linecache (~> 1.2)
      slop (~> 3.6)
    coffee-rails (4.1.0)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0, < 5.0)
    coffee-script (2.3.0)
      coffee-script-source
      execjs
    coffee-script-source (1.9.0)
    columnize (0.9.0)
    debug_inspector (0.0.2)
    debugger-linecache (1.2.0)
    erubis (2.7.0)
    execjs (2.2.2)
    globalid (0.3.2)
      activesupport (>= 4.1.0)
    hike (1.2.3)
    i18n (0.7.0)
    jbuilder (2.2.6)
      activesupport (>= 3.0.0, < 5)
      multi_json (~> 1.2)
    jquery-rails (4.0.3)
      rails-dom-testing (~> 1.0)
      railties (>= 4.2.0)
      thor (>= 0.14, < 2.0)
    json (1.8.2)
    loofah (2.0.1)
      nokogiri (>= 1.5.9)
    mail (2.6.3)
      mime-types (>= 1.16, < 3)
    mime-types (2.4.3)
    mini_portile (0.6.2)
    minitest (5.5.1)
    multi_json (1.10.1)
    nokogiri (1.6.6.2-x86-mingw32)
      mini_portile (~> 0.6.0)
    rack (1.6.0)
    rack-test (0.6.3)
      rack (>= 1.0)
    rails (4.2.0)
      actionmailer (= 4.2.0)
      actionpack (= 4.2.0)
      actionview (= 4.2.0)
      activejob (= 4.2.0)
      activemodel (= 4.2.0)
      activerecord (= 4.2.0)
      activesupport (= 4.2.0)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.2.0)
      sprockets-rails
    rails-deprecated_sanitizer (1.0.3)
      activesupport (>= 4.2.0.alpha)
    rails-dom-testing (1.0.5)
      activesupport (>= 4.2.0.beta, < 5.0)
      nokogiri (~> 1.6.0)
      rails-deprecated_sanitizer (>= 1.0.1)
    rails-html-sanitizer (1.0.1)
      loofah (~> 2.0)
    railties (4.2.0)
      actionpack (= 4.2.0)
      activesupport (= 4.2.0)
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    rake (10.4.2)
    rdoc (4.2.0)
      json (~> 1.4)
    sass (3.4.11)
    sass-rails (5.0.1)
      railties (>= 4.0.0, < 5.0)
      sass (~> 3.1)
      sprockets (>= 2.8, < 4.0)
      sprockets-rails (>= 2.0, < 4.0)
      tilt (~> 1.1)
    sdoc (0.4.1)
      json (~> 1.7, >= 1.7.7)
      rdoc (~> 4.0)
    slop (3.6.0)
    sprockets (2.12.3)
      hike (~> 1.2)
      multi_json (~> 1.0)
      rack (~> 1.0)
      tilt (~> 1.1, != 1.3.0)
    sprockets-rails (2.2.4)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      sprockets (>= 2.8, < 4.0)
    sqlite3 (1.3.10-x86-mingw32)
    thor (0.19.1)
    thread_safe (0.3.4)
    tilt (1.4.1)
    turbolinks (2.5.3)
      coffee-rails
    tzinfo (1.2.2)
      thread_safe (~> 0.1)
    tzinfo-data (1.2015.1)
      tzinfo (>= 1.0.0)
    uglifier (2.7.0)
      execjs (>= 0.3.0)
      json (>= 1.8.0)
    web-console (2.0.0)
      activemodel (~> 4.0)
      binding_of_caller (>= 0.7.2)
      railties (~> 4.0)
      sprockets-rails (>= 2.0, < 4.0)

PLATFORMS
  x86-mingw32

DEPENDENCIES
  byebug
  coffee-rails (~> 4.1.0)
  execjs (~> 2.2.2)
  jbuilder (~> 2.0)
  jquery-rails
  rails (= 4.2.0)
  sass-rails (~> 5.0)
  sdoc (~> 0.4.0)
  sqlite3
  turbolinks
  tzinfo-data
  uglifier (>= 1.3.0)
  web-console (~> 2.0)

application.rb file:

require File.expand_path(\'../boot\', __FILE__)

require \'rails/all\'

# Require the gems listed in Gemfile, including any gems
# you\'ve limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Pinplug
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run \"rake -D time\" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = \'Central Time (US & Canada)\'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join(\'my\', \'locales\', \'*.{rb,yml}\').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true
  end
end

I also downgraded the turbolinks gem from 2.3.0 to 2.2.2, thinking that the newer version of the turbolinks gem was causing my app to have errors; obviously that didn\'t work for me.

Any suggestions on what could be going wrong with my app?

回答1:

In your /app/views/layouts/application.html.erb lines 5 and 6, change the first parameter from application to default.

I met the same problem, too for my situation, I don\'t know why, but it only happens on Windows. The parameter application works on the web server.



回答2:

If you are running in WIndows, the coffee-script-source 1.9.0 does not work on windows.

Change it to a previous version adding this line to your Gemfile:

gem \'coffee-script-source\', \'1.8.0\'

And then reinstall the bundle adjusting the dependencies for the new Gem version with:

bundle update coffee-script-source


回答3:

I\'ve been having this issue for a while and having gone through all the answers in this thread and found them all to be unsuccessful, I\'ve decided to add my solution in hopes that it helps future Rails users.

I\'ve done everything in this thread - changing application to default allows it to go through without an error, but then once you actually try to use any JavaScript, an error is displayed regarding not being able to find default.js. I\'ve installed NodeJS and put it in my Ruby/bin folder. That did nothing, so I removed it.

You can indeed temporarily get around this problem with the \"switching out application with default\" solution, but it\'s extremely short term. This solution may work for you if you have no intention of styling or adding JavaScript to your app through the Rails asset pipeline. I have not tested this myself, but I imagine including JavaScript and stylesheets in the application.html header file may work. But then you lose the magic of Rails.

I have had this issue on Windows 8, Windows 10, and Ubuntu platforms.

The only thing that solves it, for me, and still renders the JavaScript and stylesheets is to first change application to application.css for the stylesheets (even if you\'re using Sass/SCSS, because it still compiles down into css) and application.js for the JavaScript link. Once that is done, go into your /assets/javascripts/application.js file and delete the // in front of //= require turbolinks.

And now your application should work just fine.

This is probably a deeper issue with turbolinks, but this is a quick workaround that hasn\'t let me down yet.



回答4:

in windows coffee-script source >= 1.9.0 not work properly. just add in Gemfile

 gem \'coffee-script-source\', \'1.8.0\'

and run

bundle update coffee-script-source


回答5:

Just install NodeJS on your local machine (make sure the correspodning entry is added to the PATH) and add

gem \'execjs\'

into the Gemfile.



回答6:

On windows 10 version 1511, adding gem \'coffee-script-source\', \'1.8.0\' to my gemfile and then ruby bin\\bundle update coffee-script-source from the project directory worked. NB: the previous coffee-script-source version that got replaced was 1.10.0



回答7:

(On Windows 8)

  1. Install Node.js!
  2. Add it to the PATH (ENV_VAR)
  3. restart the server


回答8:

I had this issue too. I went into Sublime into my project folder. Then clicked the app folders, view folder, layout, clicked application.html.erb and changed lines 5 and 6 \'application\' to \'default\'. Worked great. Thanks for posting your problem and thank you to those that answered!

I just wanted to add I went in through Sublime. I am just learning and I didn\'t understand how to access the .erb file. Just wanted to help others.



回答9:

I just changed the \'application\' to \'default\' on lines 5 and 6, and resolved. /app/views/layouts/application.html.erb.

FROM:

<%= stylesheet_link_tag    \'**application**\', media: \'all\', \'data-turbolinks-track\' => true %>
  <%= javascript_include_tag \'**application**\', \'data-turbolinks-track\' => true %>

TO:

<%= stylesheet_link_tag    \'**default**\', media: \'all\', \'data-turbolinks-track\' => true %>
  <%= javascript_include_tag \'**default**\', \'data-turbolinks-track\' => true %>


回答10:

Finally got it working what a sigh of relief. Even though I am not exactly sure what made it work. Tried all of the following. 1) Installed node.js , added coffescript npm to it, and restarted. 2) Added gem for exec.js and modified runtime.rb so that it is not able to use default windows script file. Refer to this link on how to do this: [https://github.com/sstephenson/execjs/issues/81][1] 3) To be safe, also added rubyracer gem gem \'therubyracer\', platforms: :ruby

In summary added two scripting engines , nodejs and rubyracer and added execjs so that it chooses the best scripting engine , but is not able to choose windows scrpting engine.

I guess just installing nodejs alone might have worked after adding execjs. However I did not restart(post nodejs installation) and tried all the other things.



回答11:

we should only change line 6 from application to default in /app/views/layouts/application.html.erb . changing line 5 will result in stylesheets not loading.



回答12:

You need to read the instruction in the rails page. You need ExecJS gem and some JS runtime.

More practical: 1.- Install some Runtime JS, for example node.js

2.- get ExecJS gem-> gem install execjs

Explication:

From the rails page:

\"Compiling CoffeeScript and JavaScript asset compression requires you have a JavaScript runtime available on your system, in the absence of a runtime you will see an execjs error during asset compilation. Usually Mac OS X and Windows come with a JavaScript runtime installed. Rails adds the therubyracer gem to the generated 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 the Gemfile in apps generated under JRuby. You can investigate all the supported runtimes at ExecJS.\"

From hte gitHub of ExecJS:

\"ExecJS lets you run JavaScript code from Ruby. It automatically picks the best runtime available to evaluate your JavaScript program, then returns the result to you as a Ruby object.

ExecJS supports these runtimes:

therubyracer - Google V8 embedded within Ruby
therubyrhino - Mozilla Rhino embedded within JRuby
Duktape.rb - Duktape JavaScript interpreter
Node.js
Apple JavaScriptCore - Included with Mac OS X
Microsoft Windows Script Host (JScript)
Google V8
mini_racer - Google V8 embedded within Ruby\" 

ExecJS Readme GitHub



回答13:

I tried all of the solutions above, and the only one that worked properly was to install node.js, add the directory to the \'path\' environment variable in windows (found in Advanced System Settings, Envirnment Variables, path) and then restart the computer and server. I think the later versions of coffee-script-source must be dependent on node.js, along with other gems. I had the identical error without using coffee-script-source.

Changing \'application\' to \'default\' is a terrible solution though, because it just removes all of the application\'s css styling. I do not recommend it at all.



回答14:

Its problem with turbolinks gem so its creating error

So in app\\views\\layouts\\application.html.erb change this

<%= stylesheet_link_tag    \'application.css\', media: \'all\', \'data-turbolinks-track\': \'reload\' %>
<%= javascript_include_tag \'application.js\', \'data-turbolinks-track\': \'reload\' %>

from

<%= stylesheet_link_tag    \'application\', media: \'all\', \'data-turbolinks-track\': \'reload\' %>
<%= javascript_include_tag \'application\', \'data-turbolinks-track\': \'reload\' %>

and uncomment the line no.15 in app\\assets\\javascripts\\application.js

require turbolinks

and copy it into app\\assets\\stylesheets\\application.css at the end of file

From this your css and js will be run



回答15:

I am on Rails 5.1.6. I had the same issue. As recommended above I degraded the coffee-script-source gem which solved the issue.

# Use CoffeeScript for .coffee assets and views
gem \'coffee-rails\', \'~> 4.2\' # <-- This existed already
gem \'coffee-script-source\', \'1.8.0\' # <-- Adding this line solved the problem

Note: I didn\'t modify application.html.rb file



回答16:

On Windwos 10

Just install node.js and make sure node executable is in path variable.



回答17:

(On Windows 10 - Rails 4.2.5): I solved by doing this:

1) Install nodes.js

2) Install the coffe-script-source gem

gem \'coffee-script-source\', \'1.8.0\'

3) Add this line in \"config\\initializers\\assets.rb\"

Rails.application.config.assets.precompile + =% w (application.css)