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"}
I also had a similar problem. According to your example my routes.rb looked like this
Changed it to:
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?
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.
What version of Rails are you running? Try creating a test application with just the code you have provided here and see if it works. There is probably something else causing a conflict that you have not mentioned.
Using your code on Rails 3.0.0beta4 produced the desired results:
Here is my routes file:
I have a people_controller.rb with a search method defined.