Reputation: 71
I'm trying to create routes manually for my users model ( not with "resource: users
"),
So in routes.rb
:
get '/users/:id', to: 'users#show', as: 'user'
get '/users/new', to: 'users#new', as: 'new_user'
But when I'm trying to go to /users/new
i get "Couldn't find User with id=new"
.
I do understand why it happens, but I want to find way to allow it as is (without changing one of the paths). how does it possible?
Upvotes: 2
Views: 989
Reputation: 53038
You can define your routes as below:
get '/users', to: 'users#index'
post '/users', to: 'users#create'
get '/users/new', to: 'users#new', as: 'new_user'
get '/users/:id/edit', to: 'users#edit', as: 'edit_user'
get '/users/:id', to: 'users#show', as: 'user'
put '/users/:id', to: 'users#update'
delete '/users/:id', to: 'users#destroy'
As long as /users/new
(new user path) route is defined before /users/:id
(show user path). Rails router would route them properly.
Upvotes: 2
Reputation: 38645
The order of routes is important. Because you've defined your show
route before the new
route, the show
action is getting executed with id
as new
.
Swap the position of the two routes:
get '/users/new', to: 'users#new', as: 'new_user'
get '/users/:id', to: 'users#show', as: 'user'
Upvotes: 5