Configuring non-restful routes
I can't figure out how to do this in ra开发者_如何学JAVAils 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.
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
In Rails 3, collection
is 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
or POST
request.
精彩评论