Beetlejuice
Beetlejuice

Reputation: 4425

Make collection route generic

I'm working with Rails 3.2 in a project, and I need to create a route for all controllers.

For now, the route is:

resources :people do
  collection do
    get 'search_for'
  end
end

I need thsi "search_for" action for all controllers in application.

Thanks.

Upvotes: 0

Views: 64

Answers (2)

sjain
sjain

Reputation: 23344

Simply put this above all routes as:

get ':controller/search_for'

Upvotes: 1

jvnill
jvnill

Reputation: 29599

you can use this route

match "/:controller/search_for" => redirect("/%{controller}/search_for")

UPDATE: the route above will not work and will cause a redirect loop error simply because it causes a 301 redirect to the same route. solution is to just use

match "/:controller/search_for"

be sure to place this route above all routes so routes that go to the show action will not override this route like.

Upvotes: 1

Related Questions