前提・実現したいこと
railを勉強し始めて1か月の初心者です。メモアプリ的なものを作っているのですが、config/route.rbで、post "logout" => "users#logout"と設定しているはずなのに、routing errorの画面で、No route matches [GET] "/logout"と出てしまい、postとして受け取るはずが、getとして受け取ってしまっていてエラーが発生してしまっていることが起きているので、対処法を教えていただければ幸いです。
発生している問題・エラーメッセージ
エラーメッセージ Routing Error No route matches [GET] "/logout" Rails.root: Application Trace | Framework Trace | Full Trace Routes Routes match in priority from top to bottom
該当のソースコード
ソースコード application.html.erb <!DOCTYPE html> <html> <head> <title>App1</title> <%= csrf_meta_tags %> <%= csp_meta_tag %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> </head> <body> <header> <div class="header-logo"> <%= link_to("メモアプリ(仮)", "/") %> </div> <% if @current_user %> <ul class="menu"> <li><a href="#">Menu</a> <ul class="sub"> <li><%= link_to("メモ", "/posts/form") %></li> <li><%= link_to("ストップウォッチ", "/") %></li> <li><%= link_to("現在日時", "/") %></li> <li><%= link_to("ログアウト", "/logout", {method: "post"}) %></li> </ul> </li> <li><%= link_to(@current_user.name, "/users/#{@current_user.id}") %></li> </ul> <% else %> <ul class="menu"> <li><%= link_to("新規登録", "/signup") %></li> <li><%= link_to("ログイン", "/login") %></li> </ul> <% end %> </header> <% if flash[:notice] %> <div class="flash"> <%= flash[:notice] %> </div> <% end %> <%= yield %> </body> </html> routes.rb Rails.application.routes.draw do # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html post "favorites/:post_id/create" => "favorites#create" post "favorites/:post_id/destroy" => "favorites#destroy" post "users/:id/update" => "users#update" get "users/:id/edit" => "users#edit" post "users/create" => "users#create" get "signup" => "users#new" get "users/index" => "users#index" get "users/:id" => "users#show" post "login" => "users#login" post "logout" => "users#logout" get "login" => "users#login_form" get "posts/index" => "posts#index" get "posts/form" => "posts#form" get "posts/:id" => "posts#show" post "posts/create" => "posts#create" get "posts/:id/edit" => "posts#edit" post "posts/:id/update" => "posts#update" post "posts/:id/destroy" => "posts#destroy" get "/" => "home#top" end users_controller.rb class UsersController < ApplicationController before_action :authenticate_user, {only: [:index, :show, :edit, :update]} before_action :forbid_login_user, {only: [:new, :create, :login_form, :login]} before_action :ensure_correct_user, {only: [:edit, :update]} def index @users = User.all end def show @user = User.find_by(id: params[:id]) end def new @user = User.new end def create @user = User.new( name: params[:name], email: params[:email], image_name: "default.jpg", password: params[:password] ) if @user.save session[:user_id] = @user.id flash[:notice] = "ユーザー登録完了!" redirect_to("/users/#{@user.id}") else render("users/new") end end def edit @user = User.find_by(id: params[:id]) end def update @user = User.find_by(id: params[:id]) @user.name = params[:name] @user.email = params[:email] if params[:image] @user.image_name = "#{@user.id}.jpg" image = params[:image] File.binwrite("public/user_images/#{@user.image_name}", image.read) end if @user.save flash[:notice] = "ユーザー情報を編集しました" redirect_to("/users/#{@user.id}") else render("users/edit") end end def login_form end def login @user = User.find_by(email: params[:email]) if @user && @user.authenticate(params[:password]) session[:user_id] = @user.id flash[:notice] = "ログイン成功!" redirect_to("/posts/index") else @error_message = "メールアドレスまたはパスワードが間違っています" @email = params[:email] @password = params[:password] render("users/login_form") end end def logout session[:user_id] = nil flash[:notice] = "ログアウトしました" redirect_to("/login") end def likes @user = User.find_by(id: params[:id]) @likes = Like.where(user_id: @user.id) end def ensure_correct_user if @current_user.id != params[:id].to_i flash[:notice] = "権限がありません" redirect_to("/posts/index") end end end
試したこと
application.html.erbにて、{method: "post"}を付け加えたり、controllerやroute.rbに誤字脱字がないかを確かめた。