我知道这是一个新手问题,但我没有看到解释,我想要一个。
当Rails发出如下路由错误时,它究竟意味着什么:
Routing Error
No route matches {:action=>"show", :controller=>"library_imports", :library_id=>#<Library id: 1, ...
让我感到困惑的是,消息本身显示我的请求被路由到show
控制器的library_imports
操作。如果请求URL与任何路由不匹配,会发生什么情况?
为了完整起见,我遇到的导致此错误的URL是:
http://localhost:3000/libraries/2/library_imports
哪个应该映射到“索引”操作,而不是“显示”。
config/routes.rb
的相关部分是:
Import::Application.routes.draw do
resources :libraries do
resources :library_imports
end
rake routes
输出的相关部分是:
library_library_imports GET /libraries/:library_id/library_imports(.:format) {:action=>"index", :controller=>"library_imports"}
POST /libraries/:library_id/library_imports(.:format) {:action=>"create", :controller=>"library_imports"}
new_library_library_import GET /libraries/:library_id/library_imports/new(.:format) {:action=>"new", :controller=>"library_imports"}
edit_library_library_import GET /libraries/:library_id/library_imports/:id/edit(.:format) {:action=>"edit", :controller=>"library_imports"}
library_library_import GET /libraries/:library_id/library_imports/:id(.:format) {:action=>"show", :controller=>"library_imports"}
PUT /libraries/:library_id/library_imports/:id(.:format) {:action=>"update", :controller=>"library_imports"}
DELETE /libraries/:library_id/library_imports/:id(.:format) {:action=>"destroy", :controller=>"library_imports"}
答案 0 :(得分:0)
错误消息误导了它不说的内容,而不是它所说的内容。确实没有可以从:controller => :library_imports, :action => :show, :library_id => @library
构建的路由,因为该哈希忽略了所需的:id
参数。
如果有一些提示,可能不那么令人困惑,例如(did you leave out a required parameter?
)。
注意在url_for
帮助程序中生成此错误(我相信),而不是在调度程序中生成此错误也很有用。因此,请求实际上并未被路由。