質問内容・実現したいこと
ログアウト機能を実装したい
現状発生している問題・エラーメッセージ
ログイン後に、ログアウトしたいのだが、反応がない。
検証画面では下記メッセージが表示されています。
checked runtime.lastError: Cannot find menu item with id parent Unchecked runtime.lastError: Cannot create item with duplicate id productmaps 38chrome-extension://j…/js/background.js:1 Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.
どの処理までうまく動いているのか
ユーザ登録・ログインまではできます。
その後のログアウトができません。
<参考>
id = 1
email = test@gmail.com
crypted_password = $2a$10$8.udv8gwySInWKRhUSyzoOQoj6y2GVCfRE05UBU4i/d9C8c/ppdFG
salt = 1FWR-NfMwS5DPJgpgu2H
first_name = test
last_name = test
created_at = 2023-05-09 08:27:56.435379
updated_at = 2023-05-09 08:27:56.435379
また投稿内容のアップデートもうまくできていませんが、まずはログアウトから片付けようと思っています。
該当のソースコード
class UserSessionsController < ApplicationController def new end def create @user = login(params[:email], params[:password]) if @user redirect_to posts_path, success: 'ログインに成功しました' else flash.now[:aleart] = 'ログインに失敗しました' render :new end end def destroy logout redirect_to root_path, notice: 'ログアウトされました' end end
Rails.application.routes.draw do root to: 'posts#index' get 'login', to: 'user_sessions#new' post 'login', to: 'user_sessions#create' delete 'logout', to: 'user_sessions#destroy' resources :users, only: %i[new create] # routin設定:resources resources :posts end
class PostsController < ApplicationController before_action :set_post, only: %i[show edit update destroy] # GET /posts def index @posts = Post.all end # GET /posts/1 def show @post = Post.find_by(params[:id]) end # GET /posts/new def new @post = Post.new end # GET /posts/1/edit def edit if @post.user_id != @current_user @post = Post.find(params[:id]) end end # POST /posts def create @post = current_user.posts.build(post_params) if @post.save redirect_to @post, notice: 'Post was successfully created.' else render :new end end # PATCH/PUT /posts/1 def update @post = Post.find(params[:id]) if @post.update(post_params) redirect_to @post, notice: 'Post was successfully updated.' else render :edit end end # DELETE /posts/1 # DELETE /posts/1 def destroy @post.destroy redirect_to posts_url, notice: 'Post was successfully destroyed.' end private # Use callbacks to share common setup or constraints between actions. def set_post @post = Post.find(params[:id]) end # Only allow a trusted parameter "white list" through. def post_params params.require(:post).permit(:title, :content) end end
試したこと
<1>
データベースにuser_idカラムがないのかも?と思い下記を実行しましたが、変化ありませんでした。
1: rails g migration AddUserIdToPosts user_id:integer実行
2: rails db:migrateでマイグレーションを実行
<2>
routesとコントローラがうまく紐づいていないのではないかと、最初は疑いましたが、問題なさそうだったため、コントローラの中身を見ていました。
root_pathの記載や、redirect先の誤りかなどを書きurlも参照しながらいじってみましたが特に変化がありません。
https://school.runteq.jp/v2/questions/3623
https://school.runteq.jp/v2/questions/3779
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。