使用者必須登入才能操作 course#destroy

首先按照下面修改 app/controllers/courses_controller.rb

diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb
index 47dfef9..2e8152a 100644
--- a/app/controllers/courses_controller.rb
+++ b/app/controllers/courses_controller.rb
@@ -1,5 +1,5 @@
 class CoursesController < ApplicationController
-  before_action :authenticate_user!, :only => [:new, :create, :edit, :update]
+  before_action :authenticate_user!, :only => [:new, :create, :edit, :update, :destroy]

   def index
     @courses = Course.all

測試失敗,修改測試 spec/controllers/courses_controller_spec.rb 在執行刪除操作前先登入即可:

diff --git a/spec/controllers/courses_controller_spec.rb b/spec/controllers/courses_controller_spec.rb
index 1e7c44c..0200b91 100644
--- a/spec/controllers/courses_controller_spec.rb
+++ b/spec/controllers/courses_controller_spec.rb
@@ -216,6 +216,9 @@ RSpec.describe CoursesController, type: :controller do
   end

   describe "DELETE destroy" do
+    let(:user) { create(:user) }
+    before { sign_in user }
+
     it "assigns @course" do
       course = create(:course)

results matching ""

    No results matching ""