ログイン空に他のユーザーが直接アクセスすることなできないようにするには
Q&A
class TasksController < ApplicationController
    before_action :require_user_logged_in
    before_action :correct_user, only: [:destroy, :show]
    def index
       #@tasks = Task.all
       @tasks = current_user.tasks.order(id: :desc).page(params[:page])
    end
    def show
        @task = Task.find(params[:id])
        redirect_back(fallback_location: root_path)
    end
    def new
       @task = Task.new
    end
    def create 
        @task = current_user.tasks.build(task_params)
        if @task.save
            flash[:success] = "タスクが正常に投稿されました"
            redirect_to @task  
        else 
            @tasks = current_user.tasks.order(id: :desc).page(params[:page])
            flash.now[:danger] = "タスクが投稿されませんでした"
            render :new
        end
    end
    def edit
        @task = Task.find(params[:id])
    end
    def update
         @task = Task.find(params[:id])
        if @task.update(task_params)
          flash[:success] = 'タスク は正常に更新されました'
          redirect_to @task
        else
          flash.now[:danger] = 'タスクは更新されませんでした'
          render :edit
        end
    end
    def destroy
        @task = Task.find(params[:id])
        @task.destroy
        flash[:success] = 'タスク は正常に削除されました'
        redirect_to tasks_url
    end
      private
  # Strong Parameter
  def task_params
    params.require(:task).permit(:content, :status)
  end
   def correct_user
    @task = current_user.tasks.find_by(id: params[:id])
    unless @task
      redirect_to root_url
    end
   end
end
二つ目のbefore_action に他のユーザーがCRUD操作できないようにしたいのですが、まだ何か足りないようなんですが、editお追加してみたら修正ができなくなり、ほかのを追加しても何が正解か分からないので、ご協力をお願いいたします。
0 likes

