Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring non-restful routes

I can't figure out how to do this in rails 3.0. I have a controller, products, and an action, search, and in routes.rb I've 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 complaining that the Product with id, search, can't be found for the action show. Anyone know what I'm doing wrong?

Thanks.

like image 254
user508546 Avatar asked Nov 18 '10 17:11

user508546


1 Answers

You're close.

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

Alternatively, you could also do:

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

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

  • 2.9.2 Adding Collection Routes
  • 3.7 HTTP Verb Constraints
like image 192
John Avatar answered Oct 05 '22 07:10

John