我不是想在Ruby中运行嵌入式JavaScript。我正在尝试使用支持外部JavaScript文件的标准Rails Asset Pipeline。这个rake资产:预编译失败。在答案中提到修复程序,指的是ExecJS runtimes.rb模块的补丁。它不需要安装TheRubyRacer。
rake assets:预编译RAILS_ENV = production --trace失败
环境:Ruby 2.0.0,Rails 4.0.3,Windows 8.1,PostreSQL 9.3.3,Devise 3.2.4,Rake 10.3.2,rspec-rails 3.0.1
我的应用程序似乎工作正常,就此而言。我正在继续努力,但在我做出进一步的改变之前,我决定再把它推到Github和Heroku。但是,当我尝试在我的本地系统上预编译我的资产之前,它在以前工作正常的地方失败了。从跟踪中可以看出,它在JavaScript中失败了,但我不是预编译过程的专家。我删除了自己的JavaScript代码并尝试了它,但它仍然失败了。我该如何解决这个问题?
config / application.rb是:
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module kac_app
class Application < Rails::Application
# don't generate RSpec tests for views and helpers
config.generators do |g|
g.test_framework :rspec, fixture: true
g.fixture_replacement :factory_girl, dir: 'spec/factories'
g.view_specs false
g.helper_specs false
end
# 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
# not needed at 4.0?
config.assets.initialize_on_precompile = false
# Load files in lib
config.autoload_paths += %W(#{config.root}/lib)
end
end
assets / javascript /中的唯一文件是application.js,它是:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require jquery.ui.all
//= require jquery.turbolinks
//= require turbolinks
//= require bootstrap
//= require dataTables/jquery.dataTables
//= require dataTables/jquery.dataTables.bootstrap3
//= require_tree .
资产/样式表的完整性:
application.css.scss
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require jquery.ui.core
*= require jquery.ui.theme
*= require dataTables/src/demo_table_jui
*= require_tree .
*/
framework_and_overrides.css.scss
// from railscast 329
$navbarBackground: #555;
$navbarBackgroundHighlight: #888;
$navbarText: #eee;
$navbarLinkColor: #eee;
// import the CSS framework
@import "bootstrap";
body { padding-top: 20px; }
// make all images responsive by default
img {
@extend .img-responsive;
margin: 0 auto;
}
// override for the 'Home' navigation link
.navbar-brand {
font-size: inherit;
}
// THESE ARE EXAMPLES YOU CAN MODIFY
// create your own classes
// to make views framework-neutral
.column {
@extend .col-md-6;
@extend .text-center;
}
.form {
@extend .col-md-6;
}
.form-centered {
@extend .col-md-6;
@extend .text-center;
}
.submit {
@extend .btn;
@extend .btn-primary;
@extend .btn-lg;
}
// apply styles to HTML elements
// to make views framework-neutral
main {
@extend .container;
background-color: #eee;
padding-bottom: 80px;
width: 100%;
margin-top: 51px; // accommodate the navbar
}
section {
@extend .row;
margin-top: 20px;
}
// Styles for Devise views
// using Bootstrap
// generated by the rails_layout gem
.authform {
padding-top: 30px;
max-width: 320px;
margin: 0 auto;
}
.authform form {
@extend .well;
@extend .well-lg;
padding-bottom: 40px;
}
.authform .right {
float: right !important;
}
.authform .button {
@extend .btn;
@extend .btn-primary;
}
.authform fieldset {
@extend .well;
}
#error_explanation {
@extend .alert;
@extend .alert-danger;
}
#error_explanation h2 {
font-size: 16px;
}
.button-xs {
@extend .btn;
@extend .btn-primary;
@extend .btn-xs;
}
// Tighten things up here...
.form-group {
margin-bottom: 5px; // Was 15px
}
/* line 194, D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/bootstrap-sass-3.1.1.0/vendor/assets/stylesheets/bootstrap/_forms.scss */
.radio,
.checkbox {
margin-top: 5px;
margin-bottom: 5px;
}
scaffolds.css.scss
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}
p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}
pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}
a {
color: #000;
&:visited {
color: #666;
}
&:hover {
color: #fff;
background-color: #000;
}
}
div {
&.field, &.actions {
margin-bottom: 10px;
}
}
#notice {
color: green;
}
.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}
#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff;
}
ul li {
font-size: 12px;
list-style: square;
}
}
跟踪是:
D:\BitNami\rubystack-2.0.0-11\projects\kac_app>rake assets:precompile RAILS_ENV=production --trace
** Invoke assets:precompile (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Execute assets:precompile
rake aborted!
ExecJS::RuntimeError:
(in D:/BitNami/rubystack-2.0.0-11/projects/kac_app/app/assets/javascripts/application.js)
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/execjs-2.2.0/lib/execjs/external_runtime.rb:152:in `exec_run
time'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/execjs-2.2.0/lib/execjs/external_runtime.rb:29:in `exec'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/uglifier-2.5.0/lib/uglifier.rb:186:in `really_compile'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/uglifier-2.5.0/lib/uglifier.rb:110:in `compile'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/uglifier_compressor.rb:25:in
`evaluate'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/tilt-1.4.1/lib/tilt/template.rb:103:in `render'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/context.rb:197:in `block in e
valuate'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/context.rb:194:in `each'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/context.rb:194:in `evaluate'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/bundled_asset.rb:25:in `initi
alize'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/base.rb:377:in `new'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/base.rb:377:in `build_asset'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/index.rb:94:in `block in buil
d_asset'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/caching.rb:58:in `cache_asset
'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/index.rb:93:in `build_asset'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/base.rb:287:in `find_asset'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/index.rb:61:in `find_asset'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:211:in `block in
find_asset'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:257:in `benchmark
'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:210:in `find_asse
t'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:119:in `block in
compile'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:118:in `each'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/sprockets/manifest.rb:118:in `compile'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-rails-2.0.1/lib/sprockets/rails/task.rb:60:in `blo
ck (3 levels) in define'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-2.11.0/lib/rake/sprocketstask.rb:146:in `with_logg
er'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/sprockets-rails-2.0.1/lib/sprockets/rails/task.rb:59:in `blo
ck (2 levels) in define'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:in `call'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:in `block in execute'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:in `each'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:in `execute'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:179:in `block in invoke_with_ca
ll_chain'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:165:in `invoke'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:150:in `invoke_task'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `block (2 levels)
in top_level'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `each'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `block in top_lev
el'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:115:in `run_with_threads
'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:100:in `top_level'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:78:in `block in run'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:176:in `standard_excepti
on_handling'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:75:in `run'
D:/BitNami/rubystack-2.0.0-11/ruby/lib/ruby/gems/2.0.0/gems/rake-10.3.2/bin/rake:33:in `<top (required)>'
D:/BitNami/rubystack-2.0.0-11/ruby/bin/rake:23:in `load'
D:/BitNami/rubystack-2.0.0-11/ruby/bin/rake:23:in `<main>'
Tasks: TOP => assets:precompile
答案 0 :(得分:0)
尝试将execjs和therubyracer添加到Gemfile中,然后捆绑。
gem 'execjs'
gem 'therubyracer
答案 1 :(得分:0)
此错误的实际修复方法已在SO中进行:ExecJS::RuntimeError on Windows trying to follow rubytutorial。它需要对Ruby Gem ExecJS runtimes.rb模块进行小修补,以代替其中描述的变通方法。一旦我找到它,我就认识到这个补丁是我一年前在早期版本上安装的补丁。希望下次我看到它时,我会知道它......