Ruby on Rails [Error memorandum] Routing Error No route matches [DELETE] "/ users / sign_out"

Prerequisites

ruby 2.5.1 Rails 5.2.4.4 Creating a Ruby on Rails video posting application for a school assignment.

Thing you want to do

** Logout ** with Devise feature.

Error status

  1. I set "Logout" as shown below.

app/views/layouts/application.html/erb



 <% if user_signed_in? %>
        <div class="collapse navbar-collapse" id="Navber">
          <ul class="navbar-nav mr-auto">
            <li class="nav-item">
              <%= link_to 'My page', user_path, class: "nav-link" %>
            </li>
            <li class="nav-item">
                <%= link_to 'New post', new_movie_path, class: "nav-link" %>
            </li>
            <li class="nav-item">
                <%= link_to 'Post list', movies_path, class: "nav-link" %>
            </li>
            <li class="nav-item">
              <%= link_to "Log out", destroy_user_session_path, method: :delete, class: "nav-link" %>
            </li>
          </ul>
        </div>
  1. Unfortunately, an error when logging out!

image.png

What i did

** 1. ** The route setting of the delete method seems to be an error. Check with rails routes.

rails&nbsp;routes


destroy_user_session GET /users/sign_out(.:format) devise/sessions#destroy                                          

that? The method is ** GET **!

** 2. ** Just in case, check the rails routes of other apps with devise for reference (this time, we will call it "sample").

rails&nbsp;routes(sample)



 destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy                                                               

It's ** DELETE **!

** 3. ** What's the difference? Check routes.rb!

routes.rb(sample)


delete  'tweets/:id'  => 'tweets#destroy'

routes.rb (code being created)


 resource :user, except: [:new, :create, :destroy]

...。 The sample is described by delete.

The code being created uses the resource method. And except: [: ** destroy **] ????

** 4. ** First, delete destroy of except: [: ** destroy **].

Error routes.rb


 resource :user, except: [:new, :create]

But when I log out, I get the same error.

image.png

** 5. ** Add the following! I think it's double with resourse ...

Error routes.rb


 delete 'users/:id' =>'users#destroy'

** 6. ** The error has changed! Only one line error.

image.png

** 7. ** Error says: "Cannot find destroy action for UsersController." Added the following and ** solved (no error, go to top screen after logging out!) **!

https://railstutorial.jp/chapters/log_in_log_out?version=4.2#sec-logging_out

app/controllers/users_controller.rb


 def destroy
        session.delete(:user_id)
        @current_user = nil
        redirect_to root_url

** 8. ** Check rails routes just in case.

destroy_user_session GET    /users/sign_out(.:format) devise/sessions#destroy                                                               

Hmm~. After all the method remained GET. There is a feeling of haze, but no error will occur, so we will resolve it.

I would be grateful if anyone could tell me this point (** Why can I log out even with GET? **)! !!

Summary

** Wrong (complete misunderstanding): **

** Learning: **

Reference: * Ruby on Rails Tutorial Chapter 8 Section 3 "Logout"

https://railstutorial.jp/chapters/log_in_log_out?version=4.2#sec-logging_out

That's it!

I hope it helps you solve the error!

Recommended Posts

Ruby on Rails [Error memorandum] Routing Error No route matches [DELETE] "/ users / sign_out"
Ruby on Rails Basic Memorandum
Ruby On Rails devise routing conflict
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails controller create / delete command
[Ruby on Rails] How to display error messages
[Ruby on Rails] Add and delete tags and display (success / error) messages using ajax.
Rails new in Ruby on Rails ~ Memorandum until deployment 2
[Ruby on Rails] A memorandum of layout templates
Rails new in Ruby on Rails ~ Memorandum until deployment 1
[Ruby on Rails] Post editing function (update, delete)
[Ruby on Rails] Individual display of error messages
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Ruby on Rails] Delete s3 images with Active Strage
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Ruby on Rails] undefined method ʻid'for nil: NilClass error resolution method
From Ruby on Rails error message display to Japanese localization
Delete all the contents of the list page [Ruby on Rails]
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
[Rails] Rails version upgrade error memorandum
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
[Ruby on Rails] Posting function that only logged-in users can post
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~