Configure Unconfigured Routes

I cannot figure out how to do this in rails 3.0. I have a controller, products and a search action, and in routes.rb I tried

 resources :products, :collection => {:search => :post } 

and

 match 'products/search' => 'products#search', :via => [:get, :post] 

and many other settings, but whenever I access products/search , I still get an error message indicating that the Product with id, search could not be found for the show action. Does anyone know what I'm doing wrong?

Thanks.

+6
ruby ruby-on-rails ruby-on-rails-3
source share
2 answers

You are close.

 resources :products do collection do match 'search', :via => [:get, :post] end end 

Alternatively, you can also:

 resources :products do match 'search', :on => :collection, :via => [:get, :post] end 

See Rails Routing from Out In In of the Edge Guides for more information, more specifically:

+10
source share

In Rails 3 collection now a block:

 resources :products do collection do get :search post :search end end 

This will allow you to access the ProductsController#search action using either a GET request or POST .

+4
source share

All Articles