Rails在页面#home时发生ExecJS::ProgramError错误?

68

当我创建一个控制器页面 home 并尝试访问本地主机:3000/pages/home 时,启动新应用程序时,我会收到以下错误:

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)

应用程序.html.erb文件:

<!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文件:

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文件:

class PagesController < ApplicationController
  def home
  end
end

routes.rb文件:

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 文件:

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文件:

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

我将turbolinks gem从2.3.0降级到了2.2.2,因为我认为新版本的turbolinks gem导致我的应用程序出现错误;显然,这对我没有起作用。您有什么关于我的应用程序可能出错的建议吗?

可能是重复的问题:Rails-4,ExecJS :: ProgramError in Pages#welcome - Michael Petch
在Rails 5中,这仍然是一个错误。 - Arthur
需要更多步骤。一个熟悉RoR的人为我修复了它:还需要将文件重命名为application.css.scss变成./app/assets/stylesheets/default.css.scss,application.js变成./app/assets/javascripts/default.js。然后我需要安装Node.js。最后一个微调是修改assets.rb行#Rails.application.config.assets.precompile += %w( search.js ) 为 Rails.application.config.assets.precompile += %w( default.js default.css.scss )。请注意,该行被注释了#,需要将其删除。在Windows上愉快地使用RoR。 - Arthur
18个回答

141
在您的/app/views/layouts/application.html.erb的第5和第6行,将第一个参数从application更改为default
我也遇到了同样的问题。对于我的情况,我不知道为什么,但它只发生在Windows上。在web服务器上,参数application是有效的。

3
谢谢@FlyC,我很感激! - AB10
58
在Windows系统下,coffee-script source版本号大于等于1.9.0的无法正常工作。你可以通过添加以下代码来强制使用版本号为1.8.0coffee-script sourcegem 'coffee-script-source', '1.8.0', 然后运行命令 bundle update coffee-script-source 进行更新。 - Michael Petch
2
谢谢@MichaelPetch,你的评论是对我有用的东西! - armstrhb
10
如果你检查开发者工具,就会发现由于404错误而没有包含资产文件,将“application”更改为“default”无法解决这个问题。降级coffe-script-source是正确的答案。请注意不要改变原文的意思,并尽可能使译文简洁易懂。 - e-fisher
2
我同意@excel66的观点 - 没错,我无法相信人们真的这样做或将此答案作为积极的答案,因为这并不是解决方案,因为假设您在此文件夹\assets\stylesheets中有CSS文件,它将无法加载。 - user4571629
显示剩余10条评论

59

如果您正在Windows上运行,coffee-script-source 1.9.0在Windows上无法工作。

将其更改为先前版本,将以下行添加到Gemfile中:

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

然后重新安装bundle,调整新Gem版本的依赖关系:

bundle update coffee-script-source

8
同时,重新启动服务器(这似乎很明显,但正是这个问题困扰了我)。 - Jeff
2
这个解决方案在Windows 10上有效。确保像Jeff建议的那样重新启动服务器。 - illiquent
先生您好,我按照您说的做了,但还是不起作用,为什么呢? - Angel

24

我遇到了这个问题已经有一段时间了,看了这个帖子中的所有答案,发现它们都没有成功,所以我决定添加我的解决方案,希望能帮助未来的Rails用户。

我在这个帖子中尝试了所有方法 - 将application更改为default可以让应用程序运行而不出错,但是一旦你尝试使用JavaScript,就会显示一个错误,说明找不到default.js。我安装了NodeJS并将其放在Ruby/bin文件夹中,但什么也没做,所以我把它移除了。

你确实可以通过"切换applicationdefault"的解决方案暂时地解决这个问题,但是这只是非常短期的。如果你不打算通过Rails资产管道为你的应用程序添加样式或JavaScript,那么这个解决方案可能适用于你。我自己没有测试过这个解决方案,但我想在application.html头文件中包含JavaScript和样式表可能会起作用。但是这样你就失去了Rails的魔力。

我在Windows 8、Windows 10和Ubuntu平台上遇到了这个问题。

对我来说唯一解决它并且仍然可以渲染JavaScript和样式表的方法是首先将样式表更改为application.css(即使你使用Sass/SCSS,因为它仍会编译成CSS),将JavaScript链接更改为application.js。完成后,进入/assets/javascripts/application.js文件并删除//= require turbolinks前面的//

现在你的应用程序应该可以正常工作了。

这可能是与turbolinks有关的深层次问题,但这是一个快速的解决方法,对我来说一直都起作用。


1
这个解决方案在Windows 10上对我有效,并允许Ruby看到我在“pipeline”中拥有的其他样式表。将“application”更改为“default”确实会破坏样式表。也许这只是一个Coffescript错误的原因,但这个解决方案非常好用。 - illiquent
1
哇,如果可以的话我会给你+150分。非常感谢你,这在Win 7 x64上运行良好。 - Sassy Llama
我也遇到了同样的问题,将“application”更改为“default”会抛出异常,指出找不到“application.css”。但是,这个解决方案对我有效。谢谢! - Chait

20

在Windows中,CoffeeScript源码版本大于等于1.9.0会出现问题。只需在Gemfile中添加以下内容:

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

并运行

bundle update coffee-script-source

1
非常感谢您! - nour
1
这就是解决方案!谢谢! - Angelo
1
这是我在我的Windows Server 2016中使用的解决方案。 - Jeffrey M Castro

7

只需在本地计算机上安装NodeJS(确保添加了相应的条目到PATH),然后添加

gem 'execjs'

将内容添加到Gemfile文件中。


我之前没有意识到我的电脑上没有安装NodeJS。我找到了这个视频,它基本上以相同的方式解决了问题https://www.youtube.com/watch?reload=9&v=l04kFL3pnEk,并讲述了如何在Windows机器上安装NodeJS。 - Forrest

5
在 Windows 10 版本 1511 上,将 gem 'coffee-script-source', '1.8.0' 添加到我的 gemfile 中,然后在项目目录中运行 ruby bin\bundle update coffee-script-source 就可以了。注意:之前被替换的 coffee-script-source 版本是 1.10.0。

5

(在Windows 8上)

  1. 安装Node.js!
  2. 将其添加到PATH(ENV_VAR)中
  3. 重启服务器

2
你觉得这个方案比已经被接受的一年前的答案更好的原因是什么? - TylerH
2
我认为这是一个更好的解决方案,因为在/layouts/application.html.erb中将“application”更改为“default”可能会导致CSS和JS文件报404错误。安装Node.js并不需要你对代码进行更改,从而产生不想要的结果(脚本和样式表未能加载),或者在切换到生产环境时不得不对代码进行广泛的更改。 - Jonathan Harvey
1
也适用于 Windows 10。已测试通过。 - Rafaf Tahsin

3

我也遇到了这个问题。我进入我的项目文件夹,然后点击应用程序文件夹、视图文件夹、布局,点击application.html.erb并将第5行和第6行的“application”更改为“default”。非常有效。感谢您发布您的问题,也感谢那些回答的人!

我想补充一点,我是通过Sublime进入的。我正在学习,不知道如何访问.erb文件。希望能帮助其他人。


1
我尝试了以上所有解决方案,唯一有效的方法是安装node.js,并将目录添加到Windows的“path”环境变量中(在高级系统设置、环境变量、路径中找到),然后重新启动计算机和服务器。我认为coffee-script-source的后续版本以及其他gems都必须依赖于node.js。即使没有使用coffee-script-source,我也遇到了相同的错误。
将“应用程序”更改为“默认”是一个可怕的解决方案,因为它只会移除应用程序的所有CSS样式。我完全不推荐这样做。

1
对于我的情况,这个方法适用....Node已经被安装了,但是它没有包含在环境变量 PATH 中....所以我只需将其添加到路径变量中....就这样。 - Syamsoul Azrien

1
最终解决了问题,真是松了一口气。虽然我不确定是什么让它工作了。尝试了以下所有方法: 1)安装了node.js,在其上添加了coffeescript npm,并重新启动。 2)添加了exec.js的gem,并修改了runtime.rb,使其无法使用默认的Windows脚本文件。请参考此链接: [https://github.com/sstephenson/execjs/issues/81][1] 3)为了保险起见,还添加了rubyracer gem gem' therubyracer',platforms::ruby
总之,添加了两个脚本引擎,nodejs和rubyracer,并添加了execjs,以便选择最佳的脚本引擎,但无法选择Windows脚本引擎。
我想只安装nodejs可能在添加execjs后就可以工作。但是我没有重新启动(安装nodejs后),而是尝试了所有其他方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接