Rails ExecJS::ProgramError 在 Pages#home 中?

Rails ExecJS::ProgramError in Pages#home?

启动一个新的应用程序,当我创建一个控制器页面主页并尝试转到本地 host: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)

application.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

gem文件:

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 导致我的应用程序出错;显然这对我不起作用。

对我的应用程序可能出现的问题有什么建议吗?

在你的/app/views/layouts/application.html.erb56 行,将第一个参数从 application 更改为 default.

我也遇到了同样的问题 对于我的情况,我不知道为什么,但它只发生在 Windows。 参数 application 在网络服务器上工作。

我也有这个问题。 我进入 Sublime 进入我的项目文件夹。然后单击应用程序文件夹、查看文件夹、布局,单击 application.html.erb 并将第 5 行和第 6 行 'application' 更改为 'default'。效果很好。感谢您发布问题并感谢那些回答的人!

我只是想补充一下我是通过 Sublime 进入的。我只是在学习,我不明白如何访问 .erb 文件。只是想帮助别人。

只需在本地机器上安装 NodeJS(确保将相应的条目添加到 PATH)并添加

gem 'execjs'

进入 Gemfile.

如果您在 WIndows 中 运行,coffee-script-source 1.9.0 在 windows 上不起作用。

将其更改为以前的版本,将此行添加到您的 Gem 文件中:

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

然后重新安装包,调整新 Gem 版本的依赖项:

bundle update coffee-script-source

我们只应将 /app/views/layouts/application.html.erb 中的第 6 行从 application 更改为 default。更改第 5 行将导致样式表无法加载。

我刚刚将第 5 行和第 6 行的“application”更改为“default”,并解决了。 /app/views/layouts/application.html.erb.

发件人:

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

收件人:

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

在 windows 10 版本 1511 上,添加 gem 'coffee-script-source', '1.8.0' 到我的 gem 文件,然后 ruby bin\bundle 更新咖啡脚本源 从项目目录工作。 注意:之前被替换的 coffee-script-source 版本是 1.10.0

(在 Windows 8)

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

我遇到这个问题已经有一段时间了,在浏览了这个线程中的所有答案后发现它们都不成功,我决定添加我的解决方案,希望它对未来有所帮助 Rails 用户。

我已经完成了该线程中的所有操作 - 将 application 更改为 default 可以使其顺利通过,但是一旦您实际尝试使用任何 JavaScript,显示有关无法找到 default.js 的错误。我已经安装了 NodeJS 并将其放在我的 Ruby/bin 文件夹中。那没有任何作用,所以我删除了它。

您确实可以 暂时 使用 "switching out application with default" 解决方案解决此问题,但这是非常短期的。如果您无意通过 Rails 资产管道设置样式或将 JavaScript 添加到您的应用程序,则此解决方案可能适合您。我自己没有测试过,但我想在 application.html 头文件中包含 JavaScript 和样式表可能会起作用。但是你失去了 Rails.

的魔力

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

对我来说,唯一能解决它并仍然呈现 JavaScript 和样式表的方法是首先将样式表的 application 更改为 application.css(即使您使用 Sass/SCSS,因为它仍然编译成 css) 和 JavaScript link 的 application.js。完成后,进入 /assets/javascripts/application.js 文件并删除 //= require turbolinks.

前面的 //

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

这可能是 turbolinks 的一个更深层次的问题,但这是一个尚未让我失望的快速解决方法。

终于让它工作了真松了一口气。 尽管我不确定是什么让它起作用。 尝试了以下所有内容。 1) 安装 node.js ,向其添加 coffescript npm,然后重新启动。 2) 为 exec.js 添加了 gem 并修改了 runtime.rb 以使其无法使用默认的 windows 脚本文件。 请参阅此 link 以了解如何执行此操作: [https://github.com/sstephenson/execjs/issues/81][1] 3) 为了安全起见,还添加了 rubyracer gem gem 'therubyracer',平台::ruby

总之添加了两个脚本引擎,nodejs 和 rubyracer 并添加了 execjs 以便它选择最佳脚本引擎,但无法选择 windows 脚本引擎。

我想在添加 execjs 后,仅安装 nodejs 可能会起作用。但是我没有重新启动(post nodejs 安装)并尝试了所有其他的东西。

in windows coffee-script source >= 1.9.0 无法正常工作。 只需添加 Gemfile

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

和运行

bundle update coffee-script-source

您需要阅读rails页面中的说明。你需要 ExecJS gem 和一些 JS 运行time.

更实用: 1.- 安装一些运行时 JS,例如 node.js

2.- 获取 ExecJS gem-> gem 安装 execjs

解释:

来自 rails 页面:

"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."

来自 ExecJS 的 hte gitHub:

"ExecJS 让您 运行 JavaScript 来自 Ruby 的代码。它会自动选择最佳 运行 可用时间来评估您的 JavaScript 程序,然后returns 给你的结果是一个 Ruby 对象。

ExecJS 支持这些运行次:

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

我尝试了上面的所有解决方案,唯一有效的方法是安装 node.js,将目录添加到 windows 中的 'path' 环境变量(位于Advanced System Settings, Envirnment Variables, path) 然后重启计算机和服务器。 我认为 coffee-script-source 的更高版本必须依赖于 node.js,以及其他 gem。我在没有使用 coffee-script-source 的情况下遇到了相同的错误。

将 'application' 更改为 'default' 是一个糟糕的解决方案,因为它只会删除应用程序的所有 css 样式。完全不推荐。

turbolinks 的问题 gem 所以它的创建错误

所以在 app\views\layouts\application.html.erb 中更改此

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

来自

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

并取消注释 app\assets\javascripts\application.js

中的第 15 行
require turbolinks

复制到app\assets\stylesheets\application.css文件末尾

由此你的 css 和 js 将是 运行

我在 Rails 5.1.6。我遇到过同样的问题。按照上面的建议,我降级了解决问题的 coffee-script-source gem。

# 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

注意:我没有修改application.html.rb文件

Windwos 10

只需安装 node.js 并确保节点可执行文件位于路径变量中。

(On Windows 10 - Rails 4.2.5):我这样做解决了:

1) 安装 nodes.js

2) 安装 coffe-script-source gem

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

3) 在“config\initializers\assets.rb”中添加这一行

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

对于 Windows 用户 您可以将 execjs 运行时路径设置为 Node

config/boot.rb

ENV['EXECJS_RUNTIME'] = 'Node'