我遇到的问题是我的Heroku生产网站上没有找到图片。我已将其跟踪到文件系统中的摘要与应用程序中的摘要不同。例如,在日志中我看到以下错误:
app[web.1]: Started GET "/assets/nfl_teams/nfcn/chi-6317d0375db3876a5057f2da59bab1dc.jpeg" for 24.55.52.101 at 2016-09-08 23:10:18 +0000
app[web.1]: ActionController::RoutingError (No route matches [GET] "/assets/nfl_teams/nfcn/chi-6317d0375db3876a5057f2da59bab1dc.jpeg"):
app[web.1]: vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
app[web.1]: vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
app[web.1]: vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/rack/logger.rb:38:in `call_app'
当我进入Heroku控制台并使用asset_path()查找资产时,我得到以下内容:
$ heroku run rails console
Running rails console on ⬢ fbpm... up, run.7463
Loading production environment (Rails 4.2.5)
irb(main):001:0> puts helper.asset_path("nfl_teams/nfcn/chi.jpg")
/assets/nfl_teams/nfcn/chi-93d8c8c53ac98c60f7f3fee1fade067dfa9489b24b8fa4bb9fedd4b12da129de.jpg
=> nil
irb(main):002:0>
最后查看heroku bash并检查我得到的目录中的文件:
$ heroku run bash
Running bash on ⬢ fbpm... up, run.7321
cd public~ $ cd public/assets/nfl_teams/nfcn
~/public/assets/nfl_teams/nfcn $ ls chi*.jpg
chi-93d8c8c53ac98c60f7f3fee1fade067dfa9489b24b8fa4bb9fedd4b12da129de.jpg
~/public/assets/nfl_teams/nfcn $
因此,在查看资产路径的控制台中,我得到以下文件:
/assets/nfl_teams/nfcn/chi-93d8c8c53ac98c60f7f3fee1fade067dfa9489b24b8fa4bb9fedd4b12da129de.jpg
当我查看目录结构时,我得到了相同的文件。但由于某种原因,应用程序正在寻找此文件:
/assets/nfl_teams/nfcn/chi-6317d0375db3876a5057f2da59bab1dc.jpeg
哪个不存在并引发路由错误。
这是我的Gemfile:
source 'https://rubygems.org'
ruby '2.2.4'
#ruby-gemset=Rails_fb
gem 'rails','4.2.5'
gem 'bcrypt', '3.1.11'
gem 'annotate'
gem 'faker', '1.6.3'
gem 'select2-rails'
gem 'simple_form', '3.1.1'
gem 'cocoon'
#
# Bootstrap support gems
#
gem 'bootstrap-sass'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'font-awesome-sass', '4.6.2'
gem 'font-awesome-rails'
gem 'sass-rails', '~> 5.0.4'
#gem 'select2-sass-bootstrap-rails'
# The following Gem is used to parse the NFL page for schedules to build
# a season.
gem 'nokogiri'
# Database. Using the same database for production/development
gem 'pg'
group :development, :test do
gem 'rspec-rails', '~>3.0'
gem 'factory_girl_rails', '4.7.0'
gem 'capybara', '2.7.1'
gem 'database_cleaner'
gem 'guard-rspec', '~> 4.2'
gem 'spork-rails'
gem 'guard-spork'
gem 'childprocess', '0.5.8'
gem 'letter_opener_web', '~>1.2.0'
end
group :test do
gem 'pry'
gem 'selenium-webdriver', '2.53.0'
gem 'libnotify', '0.9.1'
end
gem 'uglifier', '2.7.2'
gem 'coffee-rails', '4.1.1'
gem 'jquery-rails', '3.1.4'
gem 'jquery-turbolinks', '2.1.0'
gem 'turbolinks', '2.5.3'
gem 'jbuilder', '2.4.1'
group :doc do
gem 'sdoc', '0.4.1', require: false
end
group :production do
gem 'rails_12factor'
end
# To use debugger
# gem 'debugger'
gem 'execjs'
gem 'therubyracer', :platforms => :ruby
以下是我用来访问视图中图片的代码(我在使用和不使用asset_path()时都尝试过这样的代码):
<%= image_tag(asset_path(team.imagePath), size: "40") %>
任何人都可以告诉我为什么该应用程序正在寻找错误的文件?显然资产管道存在一些脱节,但我无法确定在哪里?
这个问题在我从Ruby 2.0.0升级到Ruby 2.2.4后开始,这导致我更新了一些我的Gems。
答案 0 :(得分:0)
希望这会有所帮助。为了在Rails 4中强制Heroku上的新资源预编译,您需要执行以下操作:
rake assets:clobber
佣金资产:预编译
然后git commit / push to master并推送到Heroku,它应该做一个新的预编译,希望能够同步资产管道。 不要使用rake资产:干净,因为它不会强制进行新的编译。