class Admin::VideosController < AdminsController
before_action :require_user
def new
end
end
-----------
class AdminsController < ApplicationController
before_action :require_admin
private
def require_admin
if !current_user.admin?
flash[:error] = "You are not allowed to access this page."
redirect_to home_path
end
end
end
------------
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user, :logged_in?
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
def logged_in?
!!current_user
end
def require_user
if !logged_in?
flash['danger'] = 'You need to sign in'
redirect_to sign_in_path
end
end
end
上面的代码是我创建一个新的控制器AdminsController
之后,把Admins::VideoController
中的require_admin
拿到AdminsController
后的结果
请问,在测试的时候 get :new
为什么先触发 require_admin
这个方法,而不是先触发require_user
呢?之前都是按照正常顺序来的。