前提・実現したいこと
一対多のタスク管理アプリを作成しており、
ログインユーザが作成したタスクがそのユーザのタスクなのかを判断させる機能を実装しています。
発生している問題・エラーメッセージ
ログインまではいいのですが、ログイン後にタスクの一覧に飛ばされるのですが、次のようなエラー画面になりました。
We're sorry, but something went wrong. If you are the application owner check the logs for more information.
コンソールを見てみると、
Completed 500 Internal Server Error
といったエラーがでており、調べたところパーミッションがどうたらと出てきたのですが、どこで設定されているのかわかりませんでした、
該当のソースコード
class TasksController < ApplicationController before_action :set_task, only:[:show, :edit, :update, :destroy] before_action :require_user_logged_in before_action :correct_user, only: [:destroy] def index @tasks = Task.all end def show set_task end def new @task = Task.new end def create @task = current_user.tasks.build(task_params) @task.user = current_user if @task.save flash[:success] = 'タスクが作成されました' redirect_to @task else flash.now[:danger] = 'タスクが作成されませんでした' render :new end end def edit set_task end def update set_task if @task.update(task_params) flash[:success] = 'タスクを編集しました' redirect_to @task else flash.now[:danger] = "タスクを編集できませんでした" render :edit end end def destroy set_task @task.destroy flash[:success] = "タスクは削除されました" redirect_to tasks_url end private # Strong Parameter def set_task @task = Task.find(params[:id]) end def task_params params.require(:task).permit(:content, :status) end def corrrect_user @task = current_user.tasks.find_by(id: params[:id]) unless @task redirect_to @task end end end
class UsersController < ApplicationController before_action :require_user_logged_in, only: [:index, :show] def index @users = User.all.page(params[:page]) end def show @user = User.find(params[:id]) end def new @user =User.new end def create @user = User.new(user_params) if @user.save flash[:success] = 'ユーザを登録しました。' redirect_to tasks_path else flash[:danger] = 'ユーザの登録に失敗しました。' end end private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end end
class ApplicationController < ActionController::Base protect_from_forgery with: :exception include SessionsHelper private def require_user_logged_in unless logged_in? redirect_to login_url end end end
rails初心者なので初歩的なことかもしれないですが、ご教授願えればと思います。
あなたの回答
tips
プレビュー