Some issues acessing asset files in production mode

蓝咒 提交于 2019-12-12 01:53:14

问题


I am using Ruby on Rails 4.1.1 and on my local machine I have the following image:

# Directory: MyApp/app/assets/images/
logo.png

I upload MyApp using the Capistrano gem and all seems to work as expected. However when I try to access my website through the browser at the following URLs

1) http://www.myapp.org/logo.png
2) http://www.myapp.org/assets/logo.png
3) http://www.myapp.org/images/logo.png
4) http://www.myapp.org/assets/images/logo.png

then I get an error page:

The page you were looking for doesn't exist.
You may have mistyped the address or the page may have moved.

In the log file I get:

1) ActionController::RoutingError (No route matches [GET] "/logo.png"):
2) ActionController::RoutingError (No route matches [GET] "/assets/logo.png"):
3) ActionController::RoutingError (No route matches [GET] "/images/logo.png"):
4) ActionController::RoutingError (No route matches [GET] "/assets/images/logo.png"):

  actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.1) lib/rails/engine.rb:514:in `call'
  railties (4.1.1) lib/rails/application.rb:144:in `call'
  railties (4.1.1) lib/rails/railtie.rb:194:in `public_send'
  railties (4.1.1) lib/rails/railtie.rb:194:in `method_missing'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.45/lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.45/lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.45/lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.45/lib/phusion_passenger/request_handler.rb:448:in `block (3 levels) in start_threads'

What is the problem and how can I solve it?


  • In my local machine (development mode) I do not have problems.
  • My remote machine runs Ubuntu 12.04 LTS, Phusion Passenger 4.0.45, Apache 2.2.22.

回答1:


All assets that are compiled in Production have a fingerprint ID appended to the file name. Therefore, in production, logo.png does not technically exist anymore. What should exist now is logo-SOME_FINGERPRINT_ID.png. See more info here:

http://guides.rubyonrails.org/asset_pipeline.html#what-is-fingerprinting-and-why-should-i-care-questionmark

Also, if you wish to serve just the static image, you will need to put it in the images dir of the public folder. You will then need to tell Apache or Nginx to serve static assets from that location.

EDIT: To link to an image in your Asset Pipeline -

link_to(LINK_TEXT_OR_IMAGE_TAG_HELPER, image_path(IMAGE_NAME))

Asset Url Helper



来源:https://stackoverflow.com/questions/27571213/some-issues-acessing-asset-files-in-production-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!