Ryzal Yusoff
Ryzal Yusoff

Reputation: 1047

How to redirect Rails old path to the new path?

So this is the path for all my users:

http://localhost:3000/users

I would like to change /users to /members , so I did this:

get '/members' => 'users#index', as: "members"

This way, people can visit http://localhost:3000/members and get all the same content in /users .

But the problem is that http://localhost:3000/users is still accessible to people. How can I remove/hide/redirect /users, so that people will see /members when they try to use this old /users url: http://localhost:3000/users ?

Upvotes: 1

Views: 574

Answers (2)

Miguel Torres
Miguel Torres

Reputation: 52

Well, it depends on if you want to eliminate the path or simply override it

Since you mention that http://localhost:3000/users still maps somewhere, I'm going to assume that you have either the line get '/users' => 'users#index' or more likely something like resources :users. If you have the first line, simply delete it, if it's the second you can restrict the routes that you create with the :except option

resources :users, except: :index

For a redirect, the solution hamdi posted should work as well.

Check out rails routings page for more info: http://guides.rubyonrails.org/routing.html

Cheers

Upvotes: 3

Hamdi Bayhan
Hamdi Bayhan

Reputation: 1833

You can redirect like the below:

get '/users', to: redirect('/members')

Also you can find more info for redirection at the below link:

http://guides.rubyonrails.org/routing.html#redirection

Upvotes: 1

Related Questions