Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails singular resource still plural?

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

like image 466
holden Avatar asked Aug 04 '11 10:08

holden


People also ask

When to use resource or resources?

Declaring a resource or resources generally corresponds to generating many default routes. resource is singular. resources is plural.

What is the difference between resources and resource in rails?

Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.


2 Answers

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

like image 94
M. Cypher Avatar answered Oct 01 '22 06:10

M. Cypher


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

like image 35
Yule Avatar answered Oct 01 '22 07:10

Yule