Rails 资源单数还是复数?
我有一条搜索路线,我想将其设为单数,但是当我指定单数路线时,它仍然会生成复数控制器路线,这是应该的样子吗?
resource :search
给我
search POST /search(.:format) {:action=>"create", :controller=>"searches"}
new_search GET /search/new(.:format) {:action=>"new", :controller=>"searches"}
edit_search GET /search/edit(.:format) {:action=>"edit", :controller=>"searches"}
GET /search(.:format) {:action=>"show", :controller=>"searches"}
PUT /search(.:format) {:action=>"update", :controller=>"searches"}
DELETE /search(.:format) {:action=>"destroy", :controller=>"searches"}
复数控制器“搜索”
我实际上只有一条路线......创建搜索:
所以我做了:match“search”=> “search#create”
我只是想知道将来我是否仍然应该保留控制器复数?铁轨3.0.9
I have a search route which I would like to make singular but when I specify a singular route it still makes plural controller routes, is this how it's supposed to be?
resource :search
Gives me
search POST /search(.:format) {:action=>"create", :controller=>"searches"}
new_search GET /search/new(.:format) {:action=>"new", :controller=>"searches"}
edit_search GET /search/edit(.:format) {:action=>"edit", :controller=>"searches"}
GET /search(.:format) {:action=>"show", :controller=>"searches"}
PUT /search(.:format) {:action=>"update", :controller=>"searches"}
DELETE /search(.:format) {:action=>"destroy", :controller=>"searches"}
Plural controller "searches"
I only have one route really... to create a search:
So I did: match "search" => "search#create"
I'm just wondering for the future if I'm still supposed to keep the controller plural? Rails 3.0.9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,事情就应该这样。引用 Rails 路由指南:
http://edgeguides.rubyonrails.org/routing.html#singular-resources
Yes, that's how it's supposed to be. Quote from the Rails Guide on Routing:
http://edgeguides.rubyonrails.org/routing.html#singular-resources
您可以通过将“search”的复数设置为不可数来解决此问题,因此在 config/initializers/inflections.rb 中
现在应该允许仅使用搜索
You could fix this by setting the plural of "search" to be uncountable so in config/initializers/inflections.rb
This should now allow search to only be used
您是否希望只生成一条路线用于创建?
如果是这样:
REST 资源的控制器名为 searchs_controller 的事实是一种约定(您可以通过使用
resource :search, :only => :create, :controller 在路由中强制使用控制器名称来更改该约定=> :search
,但不值得......)。Do you want only one route to be generated for the creation?
If so:
The fact that the controller for the REST resource is named searches_controller is a convention (that you can change, by forcing the controller's name in the route with
resource :search, :only => :create, :controller => :search
, but it does not worth it...).搜索真的是资源吗?如果是,那么您创建的是一个具有“搜索”类型的模型实例,在这种情况下,复数控制器“搜索”就非常有意义。
但是,如果它是一个没有多个模型的控制器,那么可能就不会。在这种情况下,您不需要使用
resource :search
定义路由,您只需使用get 'search/create'
告诉路由器回答“search/创建”到“搜索”控制器中的“创建”操作。Is the search really a resource? If it is, then what you a creating is an instance of a model with a type of "search", in which case the plural controller "searches" makes perfect sense.
However, if it's a controller that doesn't have multiple models, then maybe not. In which case, you don't need to define the routes with
resource :search
you can simply useget 'search/create'
to tell the router to answer "search/create" to the 'create' action in your 'search' controller.