前提・実現したいこと
Railsでwebアプリを作っています。
ログアウト機能において、headerのボタンを押すとログアウトする仕組みを実現したいです。
headerのlink_to
でmethod: :delete
と明示しているのですが、ログアウトボタンを押すと
No route matches [GET] "/logout"
のrouting errorが出てしまいます。
このエラーの解決方法を教えていただきたいです。
ソースコードは下述いたします。
程度の低い質問でしたら申し訳ございません。
また、質問に必要な情報等に不足があれば、お手数ですがご指摘いただければ、追記いたします。
よろしくお願いいたします。
該当のソースコード
html
1[_header.html.erb] 2<header> 3 <%= image_tag "logos/logo_transparent.png", class: "main-logo" %> 4 <% if logged_in? %> 5 <li class="dropdown" > 6 <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 7 <i class="fas fa-bars bars"></i> 8 </a> 9 <ul class="dropdown-menu dropdown-menu-right"> 10 <li><i class="fas fa-home color"></i><%= link_to "Home", "#" %></li> 11 <li><i class="far fa-envelope color"></i><%= link_to "DM", "#" %></li> 12 <li><i class="fas fa-search color"></i><%= link_to "Search", "#" %></li> 13 <%= link_to "Log out", "/logout", method: :delete, class: "logout btn btn-primary" %> //該当箇所ここです 14 </ul> 15 </li> 16 <!--ここにuserimage追加したい--> 17 18 <div class="menu-pc"> 19 <%= link_to "Log out", logout_path, method: :delete, class: "logout-pc d-none d-lg-inline btn btn-primary" %> 20 </div> 21 <% else %> 22 <%= link_to "Log in", login_path, class: "btn btn-primary login" %> 23 <% end %> 24</header>
ruby
1[routes.rb](ゴチャゴチャしていてすみません。コメントアウト部は無視してください。) 2Rails.application.routes.draw do 3 4 get 'sessions/new' 5 root 'static_pages#language' 6 get '/home_jp', to:'static_pages#home_jp' 7 get '/home_en', to:'static_pages#home_en' 8 get '/signup', to:'users#new' 9 get '/after_signup', to: 'users#after_signup' 10 get '/signup_jp', to:'users#signup_jp' 11 post '/signup_jp', to:'users#create' 12 # get '/details', to:'users#details' 13 # post '/details', to:'users#details_create' 14 patch 'users/:id/edit', to:'users#update' 15 get '/login', to:'sessions#new' 16 post '/login', to:'sessions#create' 17 delete '/logout', to:'sessions#destroy' #!!!該当部ここです!!! 18 # patch '/users/:id/edit_email', to:'users#update' 19 # get '/users/:id/edit_password', to:'users#edit_password' 20 # patch '/users/:id/edit_password', to:'users#update' 21 # get '/users/:id/delete', to:'users#delete', as: 'delete' 22 # delete '/users/:id/delete', to:'users#destroy' #これはresouces :usersに含まれている 23 24 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 25 # resources :sessions 26 27 resources :users do 28 member do 29 # get :edit_email 30 # patch :edit_email #updateアクションに繋げたい 31 # get :edit_password 32 # patch :edit_password #updateアクションに繋げたい 33 # get :delete #deleteアクションに繋げたい 34 # get :details 35 # post :details #details_createアクションに繋げたい 36 end 37 end 38 39 resources :details ,only: [:new, :create] 40 resources :account_activations, only: [:edit] 41 42 resources :edit_email, only: [:edit, :update] 43 44 resources :edit_password, only: [:edit, :update] 45 46 resources :delete_user, only: [:edit, :destroy] 47 48end 49
ruby
1[sessions_controller.rb] 2class SessionsController < ApplicationController 3 def new 4 end 5 6 def create 7 user = User.find_by(email: params[:session][:email].downcase ) 8 if user && user.authenticate(params[:session][:password]) 9 if user.activated? 10 log_in user 11 params[:session][:remember_me] == '1' ? remember(user) : forget(user) 12 redirect_to user 13 else 14 message = "Account not activated. " 15 message += "Check your email for the activation link." 16 flash[:warning] = message 17 redirect_to root_url 18 end 19 else 20 flash.now[:danger] = 'invalid email/password combination' 21 render 'new' 22 end 23 end 24 25 def destroy 26 log_out if logged_in? 27 redirect_to root_path 28 end 29 30 def log_in(user) 31 session[:user_id] = user.id 32 end 33end 34
ruby
1[application.js] 2// This file is automatically compiled by Webpack, along with any other files 3// present in this directory. You're encouraged to place your actual application logic in 4// a relevant structure within app/javascript and only use these pack files to reference 5// that code so it'll be compiled. 6 7require("@rails/ujs").start() 8require("turbolinks").start() 9require("@rails/activestorage").start() 10require("channels") 11require("@fortawesome/fontawesome-free/js/all") 12require("jquery") 13import "bootstrap" 14 15 16// Uncomment to copy all static images under ../images to the output folder and reference 17// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>) 18// or the `imagePath` JavaScript helper below. 19// 20// const images = require.context('../images', true) 21// const imagePath = (name) => images(name, true) 22
補足情報(FW/ツールのバージョンなど)
Rails 6.0.3
Ruby 2.6.3
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/06/30 08:10
2021/06/30 08:16
2021/06/30 08:27
2021/06/30 08:28
2021/06/30 08:29
2021/06/30 08:30
2021/06/30 08:35
2021/06/30 08:38
2021/06/30 08:42