69

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

宝石文件:

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)

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

关于我的应用程序可能出现什么问题的任何建议?

4

18 回答 18

141

在您的/app/views/layouts/application.html.erb第56 行中,将第一个参数从 更改applicationdefault

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

于 2015-02-10T16:37:14.257 回答
59

如果您在 WIndows 中运行,则 coffee-script-source 1.9.0 无法在 Windows 上运行。

将其更改为以前的版本,将此行添加到您的 Gemfile:

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

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

bundle update coffee-script-source
于 2015-05-26T14:02:44.257 回答
25

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

我已经在这个线程中完成了所有工作 - 更改applicationdefault允许它顺利通过,但是一旦你真正尝试使用任何 JavaScript,就会显示一个关于 not able to find 的错误default.js。我已经安装了 NodeJS 并将它放在我的 Ruby/bin 文件夹中。那没有任何作用,所以我将其删除。

您确实可以通过“switching out with ”解决方案暂时解决这个问题,但这是非常短期的。如果您不打算通过 Rails 资产管道为您的应用程序设置样式或添加 JavaScript,则此解决方案可能对您有用。我自己没有对此进行测试,但我想在 application.html 头文件中包含 JavaScript 和样式表可能会起作用。但是那样你就失去了 Rails 的魔力。applicationdefault

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

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

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

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

于 2016-03-13T02:12:48.217 回答
20

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
于 2016-04-26T16:36:01.037 回答
6

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

gem 'execjs'

进入Gemfile

于 2015-02-18T11:14:32.107 回答
5

(在 Windows 8 上)

  1. 安装 Node.js!
  2. 将其添加到 PATH (ENV_VAR)
  3. 重启服务器
于 2016-03-07T08:27:37.377 回答
5

在 Windows 10 版本 1511 上,将 gem 'coffee-script-source', '1.8.0' 添加到我的 gemfile 中,然后从项目目录中添加 ruby​​ bin\bundle update coffee-script-source 即可。注意:之前被替换的咖啡脚本源版本是 1.10.0

于 2016-03-03T02:14:48.587 回答
3

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

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

于 2015-02-17T16:23:50.627 回答
1

我在 Rails 5.1.6 上。我遇到过同样的问题。如上所述,我降级了解决问题的咖啡脚本源 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 文件

于 2018-06-23T15:28:32.127 回答
1

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

总之添加了两个脚本引擎,nodejs 和 ruby​​racer 并添加了 execjs 以便它选择最好的脚本引擎,但无法选择 windows 脚本引擎。

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

于 2016-04-17T06:31:01.593 回答
1

我尝试了上述所有解决方案,唯一正常工作的是安装 node.js,将目录添加到 windows 中的“路径”环境变量(在高级系统设置、环境变量、路径中找到),然后重新启动计算机和服务器。我认为咖啡脚本源的更高版本必须依赖于 node.js,以及其他 gem。在不使用咖啡脚本源的情况下,我遇到了同样的错误。

将“应用程序”更改为“默认”是一个糟糕的解决方案,因为它只是删除了应用程序的所有 css 样式。我根本不推荐它。

于 2017-01-27T15:12:00.633 回答
1

我刚刚在第 5 行和第 6 行将“”更改为“ applicationdefault,并解决了。 /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 %>
于 2015-12-03T13:08:08.910 回答
1

它与 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 将被运行

于 2017-02-02T13:38:46.143 回答
0

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

config/boot.rb

ENV['EXECJS_RUNTIME'] = 'Node'
于 2019-05-13T17:38:03.620 回答
0

Windwos 10上

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

于 2018-08-02T06:50:29.897 回答
0

(在 Windows 10 - Rails 4.2.5 上):我通过这样做解决了:

1)安装nodes.js

2)安装咖啡脚本源gem

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

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

Rails.application.config.assets.precompile + =% w (application.css)
于 2018-10-28T13:18:09.070 回答
0

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

于 2015-10-03T17:05:15.317 回答
0

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

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

2.- 获取 ExecJS gem-> gem install execjs

解释:

从导轨页面:

“编译 CoffeeScript 和 JavaScript 资产压缩需要你的系统上有一个可用的 JavaScript 运行时,如果没有运行时,你会在资产编译期间看到 execjs 错误。通常 Mac OS X 和 Windows 都安装了 JavaScript 运行时。Rails 添加了therubyracer gem 到新应用程序的注释行中生成的 Gemfile,如果需要,可以取消注释。therubyrhino 是 JRuby 用户推荐的运行时,默认添加到 JRuby 下生成的应用程序的 Gemfile 中。您可以调查所有受支持的ExecJS 的运行时。”

来自 ExecJS 的 hte gitHub:

“ExecJS 允许您从 Ruby 运行 JavaScript 代码。它会自动选择可用于评估您的 JavaScript 程序的最佳运行时,然后将结果作为 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 自述文件 GitHub

于 2016-12-02T18:22:37.393 回答