前提・実現したいこと
数日間悩んでいるので質問させて頂きます汗
ログインしている本人しかページを見れなくしたいです。
具体的には、before_action :correct_user, only: [:new, :index, :show]でnew,index,showページを制限したいです。new, indexは上手く制限できているのですが,showページが本人なのに制限されてしまいます。
エラーは出ないのですが、アクセスしたユーザーがログイン中のユーザーか確認するメソッドのcorrect_userに引っかかり、本人なのに閲覧できません。
発生している問題・エラーメッセージ
エラーメッセージ ### 該当のソースコード ```ここに言語名を入力 class ApplicationController < ActionController::Base protect_from_forgery with: :exception include SessionsHelper # アクセスしたユーザーが現在ログインしているユーザーか確認します。 def correct_user redirect_to(root_url) unless current_user?(@user) end # paramsハッシュからユーザーを取得します。 def set_user @user = User.find(params[:id]) end # paramsハッシュから投稿を取得します。 def set_task @task = Task.find(params[:id]) end class TasksController < ApplicationController before_action :set_user, only: [:new, :index] before_action :set_task, only: [:edit, :update, :destroy] before_action :set_current_user, only: [:show] before_action :logged_in_user, only: [:new, :show, :edit, :index] before_action :correct_user, only: [:new, :index, :show] def new @task = Task.new end def create @task = Task.new(user_id: @current_user.id, tasks_name: task_params[:tasks_name], contents: task_params[:contents]) if @task.save flash[:success] = "新規作成に成功しました。" redirect_to tasks_index_url(@task) else render :new end end def index @tasks = Task.where(user_id: @current_user.id).order("created_at DESC").paginate(page: params[:page], per_page: 5) end def show @user = User.find(params[:user_id]) @task = Task.find(params[:id]) end module SessionsHelper # 一時的セッションにいるユーザーを返します。 # それ以外の場合はcookiesに対応するユーザーを返します。 def current_user if (user_id = session[:user_id]) @current_user ||= User.find_by(id: user_id) elsif (user_id = cookies.signed[:user_id]) user = User.find_by(id: user_id) if user && user.authenticated?(cookies[:remember_token]) log_in user @current_user = user end end end # 渡されたユーザーがログイン済みのユーザーであればtrueを返します。 def current_user?(user) user == current_user end ルーティング refix Verb URI Pattern Controller#Action root GET / static_pages#top signup GET /signup(.:format) users#new login GET /login(.:format) sessions#new POST /login(.:format) sessions#create logout DELETE /logout(.:format) sessions#destroy tasks GET /users/:id/tasks(.:format) tasks#index POST /users/:id/tasks(.:format) tasks#create new_task GET /users/:id/tasks/new(.:format) tasks#new edit_task GET /users/:id/tasks/:id/edit(.:format) tasks#edit task PATCH /users/:id/tasks/:id(.:format) tasks#update PUT /users/:id/tasks/:id(.:format) tasks#update DELETE /users/:id/tasks/:id(.:format) tasks#destroy user_tasks_show GET /users/:user_id/tasks/:id(.:format) tasks#show users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy
試したこと
tasks#new, tasks#indexは上手く出来ているので,tasks#showのurlの設定の仕方を同じにしてみましたが上手く行きませんでした汗
忙しいと思いますがどうかご教授お願い致します。汗
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー