In the app that implemented the user management function using devise, the edit page disappeared when I created my page.
Rails 6.0.0 Ruby 2.6.5
The user edit page was not displayed as intended due to the order in which the routes were displayed.
The URI of My Page is ʻusers / (user.id), and the edit page is set to ʻusers / edit
.
When I checked params, it said " id "=>" edit "
,
It seems that the cause was that I was trying to display my page by misunderstanding edit as user.id for some reason.
Error statement
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with 'id'=edit
routes.rb
Rails.application.routes.draw do
resources :users, only: :show
devise_for :users, controllers: {
registrations: 'users/registrations'
}
end
Only the relevant part
user GET /users/:id(.:format) users#show
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
user_registration PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST /users(.:format) users/registrations#create
Changed the order of routing.
All I had to do was put resources: users, only:: show
underdevise_for: users, controllers: {registrations:'users / registrations'}
so that the edit page takes precedence.
routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: {
registrations: 'users/registrations'
}
resources :users, only: :show # devise_Changed to be described under for
end
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
new_user_registration GET /users/sign_up(.:format) users/registrations#new
#Edit page takes precedence
edit_user_registration GET /users/edit(.:format) users/registrations#edit
user_registration PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST /users(.:format) users/registrations#create
#My page is listed here
user GET /users/:id(.:format) users#show
Not limited to routing, program processing is executed from above, so it is better to implement with that in mind. I think it's okay if you understand routing well, but I wrote it because there was no article even if I looked it up.
[Rails] Routing is processed in order from the top
Recommended Posts