Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Username Routing in Rails

I am trying make a project in which /username gets redirected to that username's profile. How can I make this happen?

like image 949
Rahul Khanna Avatar asked Dec 24 '22 07:12

Rahul Khanna


1 Answers

The route would be: "get /:username", to: "users#profile"

You would change users#profile to whatever your controller action is called.

You need to make sure to put this at the end of your routes. Otherwise it will intercept all your routes.

For example, don't do the following:

get "/:username", to: "users#profile"
get "/foo", to: "pages#bar"

Because you will never be able to reach the pages#bar endpoint.

like image 87
max pleaner Avatar answered Jan 07 '23 22:01

max pleaner