我有一个名为Search
的模型和一个名为:search
的资源。我想命名我的控制器SearchController
,而不是SearchesController
。但是当我初始化一个搜索实例时,Rails假定它的路由必须是“/搜索”。
我有什么办法可以阻止这个吗?
答案 0 :(得分:1)
这应该有效:
resources :search, :as => :searches
路由网址以/search
开头,指向search
控制器并使用默认命名约定:
searches GET /search(.:format) {:controller=>"search", :action=>"index"}
POST /search(.:format) {:controller=>"search", :action=>"create"}
new_search GET /search/new(.:format) {:controller=>"search", :action=>"new"}
edit_search GET /search/:id/edit(.:format) {:controller=>"search", :action=>"edit"}
search GET /search/:id(.:format) {:controller=>"search", :action=>"show"}
PUT /search/:id(.:format) {:controller=>"search", :action=>"update"}
DELETE /search/:id(.:format) {:controller=>"search", :action=>"destroy"}
错误的原因是当表单只能访问模型实例时,它会尝试根据复数模型名称查找路径助手。在这种情况下,它尝试使用searches_path
。如果我们保留默认路由名称并仅更改URL和控制器,那么事情应该有效。
Relevant documentation(在“依靠命名路线”下)