0

我创建了一个打包为 gem 的完整引擎,它允许我快速部署 CSS、图像、JS/JQuery 项和我所有项目通用的工作用户模型。在开发模式下,这一切都运行得很好。但是,尝试预编译资产以用于生产会引发错误:

# RAILS_ENV=production bundle exec rake assets:precompile
/usr/local/bin/ruby /usr/local/bin/rake assets:precompile:all RAILS_ENV=production

RAILS_GROUPS=assets
rake aborted!
couldn't find file 'my_stock.engine'
  (in my_example_app/app/assets/javascripts/application.js:17)

Tasks: TOP => assets:precompile:primary
(See full trace by running task with --trace)
rake aborted!
Command failed with status (1): [/usr/local/bin/ruby /usr/local/bin/rake as...]

Tasks: TOP => assets:precompile
(See full trace by running task with --trace)

我花了一些时间在这个问题上进行网络搜索,但没有发现任何明显相关的东西。我希望我只是忘记在设置“my_stock.engine”或在 my_example_app 中做某事,因为这真的是我第一次尝试做这样的事情。

编辑:添加 --trace 结果

** Invoke assets:precompile (first_time)
** Execute assets:precompile
/usr/local/bin/ruby /usr/local/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Execute environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary
rake aborted!
couldn't find file 'my_stock.engine'
  (in /opt/bv_applets/meetme_manager/app/assets/javascripts/application.js:17)
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/context.rb:102:in `resolve'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/context.rb:142:in `require_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/directive_processor.rb:215:in `process_require_directive'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/directive_processor.rb:165:in `block in process_directives'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/directive_processor.rb:163:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/directive_processor.rb:163:in `process_directives'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/directive_processor.rb:97:in `evaluate'
/usr/local/lib/ruby/gems/1.9.1/gems/tilt-1.4.1/lib/tilt/template.rb:103:in `render'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/context.rb:193:in `block in evaluate'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/context.rb:190:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/context.rb:190:in `evaluate'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/processed_asset.rb:12:in `initialize'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:249:in `new'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:249:in `block in build_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:270:in `circular_call_protection'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:248:in `build_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/index.rb:93:in `block in build_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/caching.rb:19:in `cache_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/index.rb:92:in `build_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:169:in `find_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/index.rb:60:in `find_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/bundled_asset.rb:16:in `initialize'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:252:in `new'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:252:in `build_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/index.rb:93:in `block in build_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/caching.rb:19:in `cache_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/index.rb:92:in `build_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:169:in `find_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/index.rb:60:in `find_asset'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/sprockets/static_compiler.rb:19:in `block in compile'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:219:in `block in each_logical_path'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:206:in `block (2 levels) in each_file'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:196:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:196:in `each_entry'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:204:in `block in each_file'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:203:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:203:in `each_file'
/usr/local/lib/ruby/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:217:in `each_logical_path'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/sprockets/static_compiler.rb:18:in `compile'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/sprockets/assets.rake:56:in `internal_precompile'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/sprockets/assets.rake:70:in `block (3 levels) in <top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/sprockets/assets.rake:60:in `block (3 levels) in <top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
/usr/local/bin/rake:23:in `load'
/usr/local/bin/rake:23:in `<main>'
Tasks: TOP => assets:precompile:primary
rake aborted!
Command failed with status (1): [/usr/local/bin/ruby /usr/local/bin/rake as...]
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/file_utils.rb:55:in `block in create_shell_runner'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/file_utils.rb:45:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/file_utils.rb:45:in `sh'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/file_utils_ext.rb:37:in `sh'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/file_utils.rb:82:in `ruby'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/file_utils_ext.rb:37:in `ruby'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/sprockets/assets.rake:12:in `ruby_rake_task'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/sprockets/assets.rake:21:in `invoke_or_reboot_rake_task'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/sprockets/assets.rake:29:in `block (2 levels) in <top (required)>'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/usr/local/lib/ruby/gems/1.9.1/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
/usr/local/bin/rake:23:in `load'
/usr/local/bin/rake:23:in `<main>'
Tasks: TOP => assets:precompile

/编辑

EDIT2:进一步挖掘和审查错误和配置:

my_example_app/app/assets/javascripts/application.js

13: //= require jquery
14: //= require jquery_ujs
15: //= require jquery.ui.datepicker
16: //= require_tree .
17: //= require my_stock.engine

Rails 资产管道配置路径:

$ be rails c

 ------
Loading development environment (Rails 3.2.13)
1.9.3-p392 :001 > y Rails.application.config.assets.paths
---
- /my_example_app/app/assets/images
- /my_example_app/app/assets/javascripts
- /my_example_app/app/assets/stylesheets
- /my_example_app/vendor/assets/javascripts
- /my_example_app/vendor/assets/stylesheets
- /.rvm/gems/ruby-1.9.3-p392/gems/jquery-rails-3.0.4/vendor/assets/javascripts
- /.rvm/gems/ruby-1.9.3-p392/gems/jquery-ui-rails-4.0.3/app/assets/images
- /.rvm/gems/ruby-1.9.3-p392/gems/jquery-ui-rails-4.0.3/app/assets/javascripts
- /.rvm/gems/ruby-1.9.3-p392/gems/jquery-ui-rails-4.0.3/app/assets/stylesheets
- /.rvm/gems/ruby-1.9.3-p392/gems/twitter-bootstrap-rails-2.2.7/app/assets/fonts
- /.rvm/gems/ruby-1.9.3-p392/gems/twitter-bootstrap-rails-2.2.7/app/assets/images
- /.rvm/gems/ruby-1.9.3-p392/gems/twitter-bootstrap-rails-2.2.7/app/assets/javascripts
- /.rvm/gems/ruby-1.9.3-p392/gems/twitter-bootstrap-rails-2.2.7/app/assets/stylesheets
- /.rvm/gems/ruby-1.9.3-p392/gems/coffee-rails-3.2.2/lib/assets/javascripts
- /.rvm/gems/ruby-1.9.3-p392/gems/bootstrap-sass-2.3.2.1/vendor/assets/images
- /.rvm/gems/ruby-1.9.3-p392/gems/bootstrap-sass-2.3.2.1/vendor/assets/javascripts
- /.rvm/gems/ruby-1.9.3-p392/gems/bootstrap-sass-2.3.2.1/vendor/assets/stylesheets
- /my_stock.engine/app/assets/images
- /my_stock.engine/app/assets/javascripts
- /my_stock.engine/app/assets/stylesheets
 => nil
1.9.3-p392 :002 > exit

...所以“my_stock.engine”在资产路径中。

/EDIT2

提前感谢您的任何/所有帮助和建议!

4

2 回答 2

0

我一直在尝试创建一个包含一些自定义 javascript 插件的 gem。您的问题看起来是您的引擎 gem 未正确包含在您的示例应用程序 gemfile 中和/或引擎 gem 未正确安装在您的 gem 存储库中。您是否做了 agem install mystock.engine-0.0.1.gem或类似的操作,然后在您的示例应用程序上 a bundle install --local?这将确保您的自定义引擎安装在本地,然后拉入您的示例应用程序。如果是这种情况,我希望看到您的引擎资产列在“/.rvm/gems/”路径下,就像您的 gemfile 中的其他 gem 一样。祝你好运!

于 2013-08-15T19:38:19.030 回答
0

你需要问自己几个问题。您是否正确设置了引擎。我自己并没有涉足过设置 Rails 引擎,但是已经阅读了 ruby​​ on rails 指南:将资产添加到您的 gems中。它强调:

这个 gem 包含一个继承自 Rails::Engine 的引擎类。通过这样做,Rails 被告知该 gem 的目录可能包含资产,并且该引擎的 app/assets、lib/assets 和 vendor/assets 目录被添加到 Sprockets 的搜索路径中。

你可以自己看看:jQuery-rails。我对上面引用的理解告诉你,通过让你的引擎类继承 Rails 引擎,这应该继承所有 Rails 方法以及它所说的通知目录它可以包含资产。

除此之外,您在预编译时还需要考虑的另一件事是:

为了更快的资产预编译,您可以通过在 config/application.rb 中将 config.assets.initialize_on_precompile 设置为 false 来部分加载您的应用程序,尽管在这种情况下模板无法看到应用程序对象或方法。Heroku 要求这是错误的。

这提出的另一个警告是以下内容,我认为这正在发生在您身上:

如果您将 config.assets.initialize_on_precompile 设置为 false,请务必在部署之前在本地测试 rake assets:precompile。它可能会暴露您的资产引用应用程序对象或方法的错误,因为无论此标志的值如何,这些仍然在开发模式的范围内。更改此标志也会影响引擎。引擎也可以为预编译定义资产。由于未加载完整的环境,因此不会加载引擎(或其他 gem),这可能会导致资产丢失。

您可以在上面的引用中看到 1) 将此设置为 false 可能会导致错误,因此您应该在本地进行测试。此外,它指出它会影响发动机。

主要部分

引擎也可以为预编译定义资产。由于未加载完整的环境,因此不会加载引擎(或其他 gem),这可能会导致资产丢失。

因此,在说所有这些时,我相信如果您将其config.assets.intialize_on_precompile设置为 false,那么您的应用程序就不会启动。我希望这会有所帮助,因为我也确实从中学到了一些东西。

更新的答案

根据您在应用程序中包含引擎的方式,正确指定路径很重要。由于跟踪中的错误消息表明它可能不在您的应用程序中。请检查您是否my_stock_engine.js在任一路径中

app/assets/javascript/my_stock_engine.js
app/vendor/assets/javascript/my_stock_engine.js
app/lib/assets/my_stock_engine.js
my_stock_engine/app/assets/javascript/my_stock_engine.js

在您的 Gemfile 中,它还取决于您如何添加它,这很可能是via path: '/some/path'

于 2013-07-24T10:53:46.553 回答