3

好的,所以我知道我正在和这里的大男孩和女孩一起玩弄一个应用程序的前端和后端 MVC(即使 bbone 不完全是 MVC),但这是我曾经工作过的第一个真正强大的应用程序,并且不幸的是,在学术领域,没有支持,尤其是更前沿的支持,所以我提前表示感谢。

尝试使用 Rails (3.2.2) 应用程序部署到 Heroku 时出现一些错误,并且资产管道一直在引起问题。由于我在前端使用 BBone,并且 [single]page[app] 需要知道加载时图像和声音的路径,因此我不得不使用这个 gist将图像路径附加到 App.assets 以传递给application.js用于启动在前端构建的应用程序的 require.js 文件。

问题似乎是 Devise 想要:config.assets.initialize_on_precompile = false而 require 想要:config.assets.initialize_on_precompile = true和 Heroku 想要在 production.rb 文件中是: config.assets.compile = true

我得到的错误是在 gist 文件assets.js.erb中调用数据库以获取Song模型中的所有歌曲,倒数第二行

App = {};
App.assets = {
  // Returns an object containing all of asset pipeline's image paths.
  //
  // Sample:
  //
  // {
  //   avatars/missing_avatar.png: "/assets/avatars/missing_avatar.png"
  //   chosen-sprite.png: "/assets/chosen-sprite.png"
  //   circle_green.png: "/assets/circle_green.png"
  //   circle_orange.png: "/assets/circle_orange.png"
  //   circle_red.png: "/assets/circle_red.png"
  //   circle_yellow.png: "/assets/circle_yellow.png"
  //   document.png: "/assets/document.png"
  // }
  //

  // This hash is because Rails' Asset Pipeline bundles the routes to files
  // per user session, then hands that to the user's session browser, for security.
  // So we create in Ruby (erb = embedded ruby) a hash of the images to be accessed
  // in the JS.
  images: {
    <% AssetsUtil.images.each do |img| %>
      "<%= img %>" : "<%= asset_path(img) %>",
    <% end %>
  },

  // Return a formatted URL for an asset.
  //
  // Sample:
  //
  // "/assets/document/png."
  //
  path: function(name) {
    // If the file is in our images object, pull the path from there.
    if (this.images && this.images[name]) {
      return this.images[name];
    }

    // Otherwise, create a generic asset path.
    return '/assets/' + name;
  }
};

// Currently this is ALL the songs in the DB, not just the current_user 's songs 
App.songs = {
  songs: <%= Song.all.to_json.html_safe %>
};

rake assets:precompile在部署到 Heroku 之前尝试按照建议执行的完整跟踪错误是:

** Invoke assets:precompile (first_time)
** Invoke requirejs:precompile:external (first_time)
** Invoke requirejs:test_node (first_time)
** Execute requirejs:test_node
** Execute requirejs:precompile:external
/Users/me/.rvm/rubies/ruby-1.9.3-p362/bin/ruby /Users/me/.rvm/gems/ruby-1.9.3-p362/bin/rake requirejs:precompile:all RAILS_ENV=production RAILS_GROUPS=assets --trace
** Invoke requirejs:precompile:all (first_time)
** Invoke requirejs:precompile:prepare_source (first_time)
** Invoke requirejs:setup (first_time)
** Invoke assets:environment (first_time)
** Invoke requirejs:precompile:disable_js_compressor (first_time)
** Execute requirejs:precompile:disable_js_compressor
** Execute assets:environment
** Execute requirejs:setup
** Invoke requirejs:clean (first_time)
** Invoke requirejs:setup 
** Execute requirejs:clean
** Execute requirejs:precompile:prepare_source
rake aborted!
uninitialized constant Song
  (in /Users/me/dev/SoundOfFractions/app/assets/javascripts/assets.js.erb)
/Users/me/dev/SoundOfFractions/app/assets/javascripts/assets.js.erb:48:in `block in singletonclass'
/Users/me/dev/SoundOfFractions/app/assets/javascripts/assets.js.erb:65530:in `instance_eval'
/Users/me/dev/SoundOfFractions/app/assets/javascripts/assets.js.erb:65530:in `singletonclass'
/Users/me/dev/SoundOfFractions/app/assets/javascripts/assets.js.erb:65528:in `__tilt_70168709646800'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/tilt-1.3.4/lib/tilt/template.rb:144:in `call'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/tilt-1.3.4/lib/tilt/template.rb:144:in `evaluate'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/tilt-1.3.4/lib/tilt/template.rb:77:in `render'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/context.rb:177:in `block in evaluate'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/context.rb:174:in `each'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/context.rb:174:in `evaluate'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/processed_asset.rb:12:in `initialize'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:241:in `new'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:241:in `block in build_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:262:in `circular_call_protection'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:240:in `build_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/index.rb:89:in `block in build_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/caching.rb:19:in `cache_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/index.rb:88:in `build_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:163:in `find_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/index.rb:56:in `find_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/bundled_asset.rb:16:in `initialize'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:244:in `new'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:244:in `build_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/index.rb:89:in `block in build_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/caching.rb:19:in `cache_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/index.rb:88:in `build_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:163:in `find_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/index.rb:56:in `find_asset'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/requirejs-rails-0.9.1/lib/tasks/requirejs-rails_tasks.rake:94:in `block (4 levels) in <top (required)>'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:212:in `block in each_logical_path'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:200:in `block (2 levels) in each_file'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:190:in `each'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:190:in `each_entry'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:198:in `block in each_file'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:197:in `each'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:197:in `each_file'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/sprockets-2.1.3/lib/sprockets/base.rb:210:in `each_logical_path'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/requirejs-rails-0.9.1/lib/tasks/requirejs-rails_tasks.rake:92:in `block (3 levels) in <top (required)>'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:228:in `call'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:228:in `block in execute'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:223:in `each'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:223:in `execute'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:166:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:159:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:187:in `block in invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:185:in `each'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:185:in `invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:165:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:159:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:152:in `invoke'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:143:in `invoke_task'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:101:in `each'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:101:in `block in top_level'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:110:in `run_with_threads'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:95:in `top_level'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:73:in `block in run'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:70:in `run'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/bin/rake:33:in `<top (required)>'
/Users/me/.rvm/gems/ruby-1.9.3-p362/bin/rake:19:in `load'
/Users/me/.rvm/gems/ruby-1.9.3-p362/bin/rake:19:in `<main>'
Tasks: TOP => requirejs:precompile:all => requirejs:precompile:prepare_source
rake aborted!
Command failed with status (1): [/Users/me/.rvm/rubies/ruby-1.9.3-p362...]
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/file_utils.rb:53:in `block in create_shell_runner'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/file_utils.rb:45:in `call'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/file_utils.rb:45:in `sh'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/file_utils_ext.rb:40:in `sh'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/file_utils.rb:80:in `ruby'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/file_utils_ext.rb:40:in `ruby'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.2/lib/sprockets/assets.rake:12:in `ruby_rake_task'
/Users/me/.rvm/gems/ruby-1.9.3-p362/gems/requirejs-rails-0.9.1/lib/tasks/requirejs-rails_tasks.rake:85:in `block (3 levels) in <top (required)>'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:228:in `call'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:228:in `block in execute'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:223:in `each'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:223:in `execute'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:166:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:159:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:187:in `block in invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:185:in `each'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:185:in `invoke_prerequisites'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:165:in `block in invoke_with_call_chain'
/Users/me/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:159:in `invoke_with_call_chain'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/task.rb:152:in `invoke'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:143:in `invoke_task'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:101:in `each'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:101:in `block in top_level'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:110:in `run_with_threads'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:95:in `top_level'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:73:in `block in run'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/lib/rake/application.rb:70:in `run'
/Users/me/.rvm/gems/ruby-1.9.3-p362@global/gems/rake-10.0.3/bin/rake:33:in `<top (required)>'
/Users/me/.rvm/gems/ruby-1.9.3-p362/bin/rake:19:in `load'
/Users/me/.rvm/gems/ruby-1.9.3-p362/bin/rake:19:in `<main>'
/Users/me/.rvm/gems/ruby-1.9.3-p362/bin/ruby_noexec_wrapper:14:in `eval'
/Users/me/.rvm/gems/ruby-1.9.3-p362/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => assets:precompile => requirejs:precompile:external

宝石文件:

source 'https://rubygems.org'

gem 'rails', '3.2.2'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :development, :test do
  gem 'railroady'
  gem 'sqlite3'
  # For linux support
  gem 'therubyracer'
end

group :production do
  gem 'pg'
  gem 'thin'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  gem 'uglifier', '>= 1.0.3'
end

# gem "better_errors", ">= 0.2.0", :group => :development
# gem "binding_of_caller", ">= 0.6.8", :group => :development

gem 'sass-rails',   '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'

gem 'jquery-rails'
gem 'rails-backbone'
gem "bootstrap-sass", "~> 2.3.0.0"
gem 'requirejs-rails'
gem 'ejs'
gem 'devise'


gem "better_errors", ">= 0.2.0", :group => :development
gem "binding_of_caller", ">= 0.6.8", :group => :development
gem 'color_routes'
gem 'gon'

那么我应该从哪里开始配置以完成部署?

潜在信息:如果您在此处观看如何从 Railscasts 制作自定义 rake 任务,运行自定义 rake 任务时会发生相同的错误,Ryan 解释说该任务无法访问数据库,因为自定义任务需要一个=> :environment dependency(~4:15在电影中)访问 User 类。由于预编译无法访问歌曲模型,我怎么能提供相同或相似的依赖application.js file

其他信息:

据我了解,资产管道的工作方式是针对每个会话,它捆绑路径(因此,当您键入类似的内容时<%= link_to 'Sign in', new_user_path %>,rails 会在资产管道中定义这些路径,并将其提供给用户。由于 Backbone 是客户端渲染,它在从 rails 服务器传递页面后,需要知道图像和声音的路径,而我发现的唯一方法是上面列出的要点,它将“资产”捆绑到 JS,类似的方法为gem 'gon'页面提供全局变量。我当然是在新的领域,并对重组持开放态度,但它在dev模式下工作,所以我也在试图弄清楚production

4

2 回答 2

1

在 heroku 资产预编译期间,Rails 环境对您不可用。因为它们被制作一次并且不再刷新,所以告诉下一次部署。话虽如此。您应该将与动态数据库相关的调用移动到您的视图中,并通过 API 调用或通过在 json 中的 dom 中编写脚本标记来“引导”数据来呈现数据。我通常有 window.NAME_OF_APPLICATION.collections.SONGS 所以它们在全球范围内可供我使用。

例子。

:javascript
  window.lineItems = {lineItems: #{ @order.line_items.order("position").to_json( :include => :department).html_safe}, order: #{@order.to_json} }

当 Backbone 上线时,它会调用这些项目。并为他们初始化一个集合。

于 2013-05-10T20:06:51.697 回答
1

@burningpony 上面接受的答案并不完全正确。

如果您使用 labs 插件,Rails环境在 heroku 资产预编译期间可用:

https://devcenter.heroku.com/articles/labs-user-env-compile

于 2013-08-01T05:24:22.273 回答