Robin van Dijk
Robin van Dijk

Reputation: 827

Change url for a route in Rails

I have a search scope for my users with the following route:

resources :users do
  collection do
    get :search
  end
end

This however generates /users/search as url. I would like to have /search as url. I tried the following:

get '/search', as: :search
get '/search' => 'users#search', as: :search
get :search, to: 'users#search', as: :search

They don't seem to work since I keep getting routing errors. What would be the correct way to write it?

Upvotes: 0

Views: 270

Answers (2)

kyle43982
kyle43982

Reputation: 44

you can also use match:

match "/search", to: "users#search", via: "get"

Upvotes: 0

Baldrick
Baldrick

Reputation: 24350

This one should work (without the leading '/') :

resources :users
get 'search' => 'users#search', as: :search

The named helpers for this route will be search_path and search_url

Upvotes: 1

Related Questions