开发者

How to prevent pluralization of a model in Rails?

开发者 https://www.devze.com 2023-02-04 21:05 出处:网络
I have model called Search and a resource named :search. I\'d like to name my controller SearchController, rather than SearchesController. But when I initialize an instance of Search, Rails assumes it

I have model called Search and a resource named :search. I'd like to name my controller SearchController, rather than SearchesController. But when I initialize an instance of Search, Rails assumes its route has to be "/searches".

Is there anything I 开发者_开发技巧can do to stop this?


This should work:

resources :search, :as => :searches

Route urls start with /search, point to search controller and use default naming convention:

   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"}

The reason for error is that when form has only access to model instance it tries to find a route helper based on pluralized model name. In this case it tried to use searches_path. Things should work if we keep the default route names and change only urls and controller.

Relevant documentation (under "Relying on named routes")

0

精彩评论

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