私はredirect_toがどのように機能するか混乱しています...以下のコードには、何があってもリダイレクトするauthorizeメソッドがあります(私の質問の例として、これは私のアプリケーションにifステートメントがあります)。次に、UsersController で、authorize メソッドを拡張し、その下にいくつかのロジックを追加します。
username
「 nil:NilClass の未定義メソッド」というエラーが表示されます。(まあ、 current_user は定義されていませんが、このステートメントの前にリダイレクトが必要です)
私の質問は、「スーパー」への呼び出しが何があってもリダイレクトする必要があるときに、UsersController 承認メソッドのコードが実行され、そのコードが実行されるべきではないのはなぜですか?
class ApplicationController < ActionController::Base
before_filter :authorize
def authorize
redirect_to root_path, alert: 'Not authorized'
return false
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
class UsersController < ApplicationController
def authorize
super
return true if current_user.username == 'admin'
end
end