When I created a user my page, the user editing function of devise became strange.

Overview

In the app that implemented the user management function using devise, the edit page disappeared when I created my page.

environment

Rails 6.0.0 Ruby 2.6.5

Cause

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

The routing looks like this

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

solution

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

Summary

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.

reference

[Rails] Routing is processed in order from the top

Recommended Posts

When I created a user my page, the user editing function of devise became strange.
Incident (rails) that is logged out when user editing of the application
Implementation of user authentication function using devise (2)
Creating a user authentication function using devise
Implementation of user authentication function using devise (1)
Implementation of user authentication function using devise (3)
[Rails] devise customization. How to change the redirect page after user registration and editing, and how to skip password input when editing
What I tried when I wanted to get all the fields of a bean
I managed to get a blank when I brought the contents of Beans to the textarea
When creating the top page, I was stuck with the handling of images (super beginner)
I tried using the Server Push function of Servlet 4.0
[Java] When writing the source ... A memorandum of understanding ①
I wrote a sequence diagram of the j.u.c.Flow sample
I learned about the existence of a gemspec file
Rails Tutorial Extension: I created a follower notification function
I want to add a delete function to the comment function
When I switched to IntelliJ, I got a lot of differences in the encoding of the properties file.
I made a reply function for the Rails Tutorial extension (Part 4): A function that makes the user unique