44

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

holden
  • 13,471
  • 22
  • 98
  • 160

4 Answers4

45

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

M. Cypher
  • 6,966
  • 2
  • 34
  • 34
  • ah ok, also I still need to specify @search + :url => search_path or the form_for won't work on a singular route? – holden Aug 04 '11 at 10:33
  • I would expect it to work without the :url parameter. But just try it out I suppose. – M. Cypher Aug 04 '11 at 10:39
  • 56
    You can do `resource :search, :controller => :search`. – Mario Uher Aug 04 '11 at 11:19
  • 1
    On a side note, I have a model called "dives" and rails made the singular "dife" which is not correct. To fix this, I had to update the Infections.rb file with "inflect.irregular 'dive', 'dives'". Now routes are correctly /dive/new instead of /dife/new. – Ryan May 04 '12 at 23:44
27

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

Yule
  • 9,668
  • 3
  • 51
  • 72
  • Thanks for the answer. This seemed to be the easiest way to avoid the singular/plural confusion. The singular/plural distinction might make sense when a controllers is associated with a model, as per the Rails convention, but when that's not the case, I see no sense for Rails automagically pluralizing identifiers. – Teemu Leisti Jun 07 '13 at 13:49
  • 2
    The problem with this is that if you use the overridden word anywhere else it will no longer be correctly pluralized. – Jacob Lockard Jul 06 '20 at 22:02
  • Making a global change to fix a local issue doesn't usually end well. Now, `search` will not get pluralized anywhere in the app. – Daniel Mar 13 '23 at 01:21
7

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

Andrea Salicetti
  • 2,423
  • 24
  • 37
6

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.

Matt Connolly
  • 9,757
  • 2
  • 65
  • 61