Ruby-on-rails rails 单数资源还是复数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6939688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:43:03  来源:igfitidea点击:

rails singular resource still plural?

ruby-on-railsruby-on-rails-3routescustom-routessingular

提问by holden

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"

所以我做了: 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

我只是想知道将来是否仍然应该保留控制器复数?导轨 3.0.9

采纳答案by M. Cypher

Yes, that's how it's supposed to be. Quote from the Rails Guide on Routing:

是的,这就是它应该的样子。引自 Rails 指南中的路由:

Because you might want to use the same controller for a singular route (/account) and a plural route (/accounts/45), singular resources map to plural controllers.

因为您可能希望对单数路由 (/account) 和复数路由 (/accounts/45) 使用相同的控制器,所以单一资源映射到多个控制器。

http://edgeguides.rubyonrails.org/routing.html#singular-resources

http://edgeguides.rubyonrails.org/routing.html#singular-resources

回答by Yule

You could fix this by setting the plural of "search" to be uncountable so in config/initializers/inflections.rb

您可以通过在 config/initializers/inflections.rb 中将“搜索”的复数设置为不可数来解决此问题

ActiveSupport::Inflector.inflections do |inflect|
   inflect.uncountable %w( search )
end

This should now allow search to only be used

这现在应该只允许使用搜索

回答by Matt Connolly

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 :searchyou can simply use get 'search/create'to tell the router to answer "search/create" to the 'create' action in your 'search' controller.

但是,如果它是一个没有多个模型的控制器,那么也许没有。在这种情况下,您不需要定义路由,resource :search您可以简单get 'search/create'地告诉路由器对“搜索”控制器中的“创建”操作回答“搜索/创建”。

回答by Andrea Salicetti

Do you want only one route to be generated for the creation?

您是否只想为创作生成一条路线?

If so:

如果是这样的话:

resource :search, :only => :create

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...).

REST 资源的控制器名为 search_controller 的事实是一个约定(您可以更改,通过强制在路由中使用 控制器的名称resource :search, :only => :create, :controller => :search,但它不值得......)。