Rails 资源单数还是复数?

发布于 2024-11-27 15:21:47 字数 859 浏览 3 评论 0原文

我有一条搜索路线,我想将其设为单数,但是当我指定单数路线时,它仍然会生成复数控制器路线,这是应该的样子吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沉默的熊 2024-12-04 15:21:47

是的,事情就应该这样。引用 Rails 路由指南:

因为您可能想对单一路线使用相同的控制器
(/account) 和复数路线 (/accounts/45),单数资源地图
到多个控制器。

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

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

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.

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

标点 2024-12-04 15:21:47

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

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

现在应该允许仅使用搜索

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

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

This should now allow search to only be used

洋洋洒洒 2024-12-04 15:21:47

您是否希望只生成一条路线用于创建?

如果是这样:

resource :search, :only => :create

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

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

半寸时光 2024-12-04 15:21:47

搜索真的是资源吗?如果是,那么您创建的是一个具有“搜索”类型的模型实例,在这种情况下,复数控制器“搜索”就非常有意义。

但是,如果它是一个没有多个模型的控制器,那么可能就不会。在这种情况下,您不需要使用 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 use get 'search/create' to tell the router to answer "search/create" to the 'create' action in your 'search' controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文