Question

The users' show action URL is "domain.com/users/(:id)". How can I change this into "domain.com/(:id)"?

(I use devise if it does make any difference.)

I added this in my routes but it doesn't work.

 get '(:username)' => 'users#show'

Couldn't figure out how is this possible in the Rails Routing guide.


Update

I placed get "/:username" => 'users#show' at the bottom of my routes file. When I browse to http://localhost:3000/testuser I get this in my logs:

Started GET "/testuser" for 127.0.0.1 at 2014-03-27 12:05:32 +0000
Processing by UsersController#show as HTML
  Parameters: {"username"=>"testuser"}
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."username" IS NULL LIMIT 1
Redirected to http://localhost:3000/
Completed 302 Found in 5ms (ActiveRecord: 0.7ms)

testuser does exist. But for some reason is nul and redirects to homepage. If I try to go to http://localhost:3000/users/testuser the user's show action loads up fine.

Was it helpful?

Solution

put

get "/:username" => 'users#show'

above root 'some_path'

update,

The route file match from up to bottom, let's say, you now have a route

 get "/search" => "controller#method"

if you put your get "/:username" before it, then your app may think you are trying to find a user with username "search", so put the get "/:username" => 'users#show' at the bottom of your routes list.

OTHER TIPS

I had to put the route get "/:id" => 'users#show' in my routes.rb file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top