开发者

I can't serve static images on Ruby on Rails

I'm trying to access:

http://localhost:3000/images/Header.png开发者_开发技巧

but I keep getting this error:

Routing Error

No route matches "/images/Header.png" with {:method=>:get}

And here are my routes:

ActionController::Routing::Routes.draw do |map|
  map.resources :worker_antpile_statuses

  map.resources :worker_antpiles

  map.resources :antcolonies

  map.resources :antpiles

  map.resources :clients
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end


Static assets need to go in the /public folder or else you'll get the routing error. For a static image at http://localhost:3000/images/Header.png you want to place Header.png in RAILS_ROOT/public/images

When given any url, Rails will check for a file existing at the path from RAILS_ROOT/public directory before attempting to match any routes.

For example when a Rails server receives a request for http://localhost:3000/users/1 it will attempt to send the contents of RAILS_ROOT/public/users/1. If no file exists, the route matching routine kicks in.


The last two lines of your routes.rb are catch-all routes that will match any previously unmatched URLs. Comment out those two lines and it will work, which is what you want to do when you use RESTful routing in all your application.

See this excellent guide for all you need to know about Rails routes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜