Rails图像资源不会显示在Heroku上

时间:2015-07-28 10:43:29

标签: ruby-on-rails ruby ruby-on-rails-4 heroku

我一直在预编译并将我的项目推向生产,并且不会出现项目范围的图像。

以下是我在应用程序中使用图像的方法。

application.html.erb

<!-- BRAND + INFO -->
  <div class="container-fluid content-buffer bg-red">
    <div class="container">
      <%= image_tag("pontiac_brand", class: "img-responsive, center-block") %>
      <h1 class="text-center page-title">Canadian Poncho Registry</h1> 
    </div>
  </div>

gemfile.rb

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

production.rb

 config.serve_static_assets = true
end

微量

▶ heroku logs --tail
2015-07-27T18:35:25.272725+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/engine.rb:518:in `call'
2015-07-27T18:35:25.272729+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/content_length.rb:15:in `call'
2015-07-27T18:35:25.272712+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:68:in `tagged'
2015-07-27T18:35:25.272718+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
2015-07-27T18:35:25.272730+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service'
2015-07-27T18:35:25.272733+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
2015-07-27T18:35:25.272736+00:00 app[web.1]: 
2015-07-27T18:35:25.272715+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2015-07-27T18:35:25.272727+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/application.rb:164:in `call'
2015-07-27T18:35:25.212502+00:00 app[web.1]: Processing by WelcomeController#about as HTML
2015-07-27T18:35:25.272709+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2015-07-27T18:35:25.272721+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/static.rb:113:in `call'
2015-07-27T18:35:25.272714+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:20:in `call'
2015-07-27T18:35:25.217403+00:00 app[web.1]: Completed 200 OK in 4ms (Views: 3.6ms | ActiveRecord: 0.0ms)
2015-07-27T18:35:25.272711+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:26:in `tagged'
2015-07-27T18:35:25.272728+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
2015-07-27T18:35:25.272698+00:00 app[web.1]: 
2015-07-27T18:35:25.272731+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
2015-07-27T18:35:25.272734+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
2015-07-27T18:35:25.272701+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/images/pontiac_brand"):
2015-07-27T18:35:25.272724+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/ssl.rb:24:in `call'
2015-07-27T18:35:25.281440+00:00 app[web.1]: Started GET "/images/pontiac_brand_footer" for 70.49.38.236 at 2015-07-27 18:35:25 +0000
2015-07-27T18:35:25.272705+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2015-07-27T18:35:25.272723+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
2015-07-27T18:35:25.272706+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:38:in `call_app'
2015-07-27T18:35:25.272716+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
2015-07-27T18:35:25.272720+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2015-07-27T18:35:25.272735+00:00 app[web.1]: 
2015-07-27T18:35:25.414798+00:00 heroku[router]: at=info method=GET path="/images/pontiac_brand_footer" host=canadianponcho.herokuapp.com request_id=ddd1bb9a-adc9-4c62-8db1-9dea13fe1e25 fwd="70.49.38.236" dyno=web.1 connect=5ms service=137ms status=404 bytes=1874
2015-07-27T18:35:25.215191+00:00 app[web.1]:   Rendered welcome/about.html.erb within layouts/application (0.4ms)
2015-07-27T18:35:25.283454+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:20:in `block in call'
2015-07-27T18:35:25.283465+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2015-07-27T18:35:25.283473+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
2015-07-27T18:35:25.283482+00:00 app[web.1]: 
2015-07-27T18:35:25.283446+00:00 app[web.1]: 
2015-07-27T18:35:25.283453+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:38:in `call_app'
2015-07-27T18:35:25.283466+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/static.rb:113:in `call'
2015-07-27T18:35:27.338099+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/application.rb:164:in `call'
2015-07-27T18:35:27.338100+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
2015-07-27T18:35:27.338104+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
2015-07-27T18:35:27.338105+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
2015-07-27T18:35:27.324494+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:26:in `tagged'
2015-07-27T18:35:27.324516+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
2015-07-27T18:35:27.324527+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/content_length.rb:15:in `call'
2015-07-27T18:35:27.324534+00:00 app[web.1]: 
2015-07-27T18:35:27.247186+00:00 app[web.1]:   Vehicle Load (2.6ms)  SELECT "vehicles".* FROM "vehicles"  ORDER BY created_at DESC
2015-07-27T18:35:27.324513+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2015-07-27T18:35:27.324525+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/application.rb:164:in `call'
2015-07-27T18:35:27.324479+00:00 app[web.1]: 
2015-07-27T18:35:27.255673+00:00 app[web.1]: Completed 200 OK in 45ms (Views: 32.8ms | ActiveRecord: 9.2ms)
2015-07-27T18:35:27.324492+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2015-07-27T18:35:27.324532+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
2015-07-27T18:35:27.324536+00:00 app[web.1]: 
2015-07-27T18:35:27.338079+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:38:in `call_app'
2015-07-27T18:35:27.338084+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:26:in `tagged'
2015-07-27T18:35:27.338097+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/engine.rb:518:in `call'
2015-07-27T18:35:27.338101+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/content_length.rb:15:in `call'
2015-07-27T18:35:27.324488+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2015-07-27T18:35:27.324491+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:20:in `block in call'
2015-07-27T18:35:27.324495+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:68:in `tagged'
2015-07-27T18:35:27.210767+00:00 app[web.1]: Processing by VehiclesController#index as HTML
2015-07-27T18:35:27.338096+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/ssl.rb:24:in `call'
2015-07-27T18:35:27.206972+00:00 app[web.1]: Started GET "/vehicles" for 70.49.38.236 at 2015-07-27 18:35:27 +0000
2015-07-27T18:35:27.324490+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:38:in `call_app'
2015-07-27T18:35:27.338078+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2015-07-27T18:35:27.338106+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
2015-07-27T18:35:27.338095+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
2015-07-27T18:35:27.338071+00:00 app[web.1]: 
2015-07-27T18:35:27.338076+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2015-07-27T18:35:27.338089+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
2015-07-27T18:35:27.338082+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2015-07-27T18:35:27.338086+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:20:in `call'
2015-07-27T18:35:27.338091+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
2015-07-27T18:35:27.338107+00:00 app[web.1]: 
2015-07-27T18:35:27.253797+00:00 app[web.1]:   Rendered vehicles/index.html.erb within layouts/application (38.2ms)
2015-07-27T18:35:27.338081+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:20:in `block in call'
2015-07-27T18:35:27.338088+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2015-07-27T18:35:27.338092+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2015-07-27T18:35:27.338109+00:00 app[web.1]: 
2015-07-27T18:35:27.324520+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
2015-07-27T18:35:27.324530+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
2015-07-27T18:35:27.324533+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
2015-07-27T18:35:27.343424+00:00 heroku[router]: at=info method=GET path="/images/pontiac_brand" host=canadianponcho.herokuapp.com request_id=7d85bf4a-3ae7-4fa6-b93a-f5c435b7c336 fwd="70.49.38.236" dyno=web.1 connect=2ms service=15ms status=404 bytes=1874
2015-07-27T18:35:27.262943+00:00 heroku[router]: at=info method=GET path="/vehicles" host=canadianponcho.herokuapp.com request_id=319158ef-4399-4fe1-a925-c94f81fc8277 fwd="70.49.38.236" dyno=web.1 connect=1ms service=56ms status=200 bytes=9123
2015-07-27T18:35:27.322291+00:00 app[web.1]: Started GET "/images/pontiac_brand_footer" for 70.49.38.236 at 2015-07-27 18:35:27 +0000
2015-07-27T18:35:27.338074+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/images/pontiac_brand"):
2015-07-27T18:35:27.338085+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:68:in `tagged'
2015-07-27T18:35:27.324511+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:20:in `call'
2015-07-27T18:35:27.324529+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service'
2015-07-27T18:35:27.336075+00:00 app[web.1]: Started GET "/images/pontiac_brand" for 70.49.38.236 at 2015-07-27 18:35:27 +0000
2015-07-27T18:35:27.324483+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/images/pontiac_brand_footer"):
2015-07-27T18:35:27.324518+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2015-07-27T18:35:27.324523+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.2.1/lib/rails/engine.rb:518:in `call'
2015-07-27T18:35:27.324526+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
2015-07-27T18:35:27.324486+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2015-07-27T18:35:27.324514+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
2015-07-27T18:35:27.324519+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/static.rb:113:in `call'
2015-07-27T18:35:27.324522+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/ssl.rb:24:in `call'
2015-07-27T18:35:27.328583+00:00 heroku[router]: at=info method=GET path="/images/pontiac_brand_footer" host=canadianponcho.herokuapp.com request_id=9842cf5c-9110-4c67-a8dd-324d696003e8 fwd="70.49.38.236" dyno=web.1 connect=0ms service=8ms status=404 bytes=1874
2015-07-27T18:35:27.338093+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/static.rb:113:in `call'
2015-07-27T18:35:27.338102+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service'

我确实预先编译了我的资产,

▶ rake assets:precompile RAILS_ENV=production
I, [2015-07-27T14:13:58.419576 #75739]  INFO -- : Writing /Users/jon/code/CanadianPoncho/public/assets/main-b51ce06efa196bd96a7f5ab915d47a4509764af7ec74ca963ad08be9b71c6221.css
I, [2015-07-27T14:13:58.423086 #75739]  INFO -- : Writing /Users/jon/code/CanadianPoncho/public/assets/pontiac_brand-a2f912ca09d2e4d1d27c65e802f69f6a0a94e976dd853e1c4aa5ae1722cdf7d8.png
I, [2015-07-27T14:13:58.426421 #75739]  INFO -- : Writing /Users/jon/code/CanadianPoncho/public/assets/pontiac_brand_footer-d0aa246137c2229a6c78617d4c6032869aa144e8c6da0b78d1c780cb493e5e60.png
I, [2015-07-27T14:14:01.601092 #75739]  INFO -- : Writing /Users/jon/code/CanadianPoncho/public/assets/application-eef7101d815d406ac703e95198e442bf3df2e274ced0dad0ad1255a2607a3c90.js
I, [2015-07-27T14:14:05.411045 #75739]  INFO -- : Writing /Users/jon/code/CanadianPoncho/public/assets/application-7ce10289fea13c0bbac37426da79b0ea83918a35aff76ffb223a6aa3ca830a86.css
I, [2015-07-27T14:14:05.415813 #75739]  INFO -- : Writing /Users/jon/code/CanadianPoncho/public/assets/bootstrap/glyphicons-halflings-regular-13634da87d9e23f8c3ed9108ce1724d183a39ad072e73e1b3d8cbf646d2d0407.eot
I, [2015-07-27T14:14:05.416893 #75739]  INFO -- : Writing /Users/jon/code/CanadianPoncho/public/assets/bootstrap/glyphicons-halflings-regular-42f60659d265c1a3c30f9fa42abcbb56bd4a53af4d83d316d6dd7a36903c43e5.svg
I, [2015-07-27T14:14:05.417699 #75739]  INFO -- : Writing /Users/jon/code/CanadianPoncho/public/assets/bootstrap/glyphicons-halflings-regular-e395044093757d82afcb138957d06a1ea9361bdcf0b442d06a18a8051af57456.ttf
I, [2015-07-27T14:14:05.418365 #75739]  INFO -- : Writing /Users/jon/code/CanadianPoncho/public/assets/bootstrap/glyphicons-halflings-regular-a26394f7ede100ca118eff2eda08596275a9839b959c226e15439557a5a80742.woff
I, [2015-07-27T14:14:05.418991 #75739]  INFO -- : Writing /Users/jon/code/CanadianPoncho/public/assets/bootstrap/glyphicons-halflings-regular-fe185d11a49676890d47bb783312a0cda5a44c4039214094e7957b4c040ef11c.woff2

1 个答案:

答案 0 :(得分:5)

你需要做两件事来解决它。首先,在production.rb文件中将这两行从false更改为true。

  config.assets.compile = true
  config.assets.digest = true

其次,如果你的图像有这样的语法

background: url("imgo.jpg") 

将其更改为

 background: image-url("image.jpg")

我希望它能完成你的工作。