Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ignores collection route and goes with show action instead

I have the following routes set up in my routes.rb file:

resources :people do 
  collection do
    get :search
  end
end

When i do a get action on the url: http://localhost:3000/people/search.json?term=stepeb, the server reports that it's responding with the show action, with the correct term parameter, but also has an id parameter, set to "search".

The problem, as i see it, are the two urls the show url would be:

/people/:id

and i believe that the router is matching that route before it gets to /people/search

If that is the case, how would collection based routes ever work? Wouldnt they all get caught by the show action?

The relevant part of rake routes is as follows:

 search_people GET      /people/search(.:format)                                                         {:action=>"search", :controller=>"people"}
                                     GET      /people(.:format)                                                                {:action=>"index", :controller=>"people"}
                              people POST     /people(.:format)                                                                {:action=>"create", :controller=>"people"}
                          new_person GET      /people/new(.:format)                                                            {:action=>"new", :controller=>"people"}
                                     GET      /people/:id(.:format)                                                            {:action=>"show", :controller=>"people"}
                                     PUT      /people/:id(.:format)                                                            {:action=>"update", :controller=>"people"}
                              person DELETE   /people/:id(.:format)                                                            {:action=>"destroy", :controller=>"people"}
                         edit_person GET      /people/:id/edit(.:format)                                                       {:action=>"edit", :controller=>"people"}
like image 812
midas06 Avatar asked Jun 23 '10 06:06

midas06


2 Answers

Doh, forget this one. Turns out i had a duplicate resources :people line at the top of the routes file. Rails was hitting that first. Seems to me there really should be a check for duplicate route definition in there.

like image 191
midas06 Avatar answered Oct 20 '22 08:10

midas06


I also had a similar problem. According to your example my routes.rb looked like this

resources :people

...

resources :people do
    collection do
      get :search
    end
  end

Changed it to:

resources :people do
    collection do
      get :search
    end
  end

...

resources :people

and i can access the collection... btw, is this the appropriate way of adding routes? i.e. is it good style to just add a new route when adding an action to a controller and leaving the "old" resources :people like it is?

like image 31
Tobi89 Avatar answered Oct 20 '22 08:10

Tobi89