我不能在Ruby on Rails上提供静态图像

sam*_*sam 7 ruby-on-rails

我正在尝试访问:

http://localhost:3000/images/Header.png
Run Code Online (Sandbox Code Playgroud)

但我一直收到这个错误:

Routing Error

No route matches "/images/Header.png" with {:method=>:get}
Run Code Online (Sandbox Code Playgroud)

以下是我的路线:

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
Run Code Online (Sandbox Code Playgroud)

Bro*_*ell 17

静态资产需要进入/ public文件夹,否则您将收到路由错误.对于http://localhost:3000/images/Header.png想要放置Header.png 的静态图像RAILS_ROOT/public/images

当给出任何URL时,Rails将在尝试匹配任何路由之前检查RAILS_ROOT/public目录中存在的文件.

例如,当Rails服务器收到请求时,http://localhost:3000/users/1它将尝试发送内容RAILS_ROOT/public/users/1.如果不存在文件,则路由匹配例程将启动.


JRL*_*JRL 1

你的最后两行routes.rb是包罗万象的路由,将匹配任何以前不匹配的 URL。注释掉这两行,它就会起作用,这就是您在所有应用程序中使用 RESTful 路由时想要做的事情。

请参阅这份出色的指南,了解您需要了解的有关 Rails 路线的所有信息。