我有保護功能,獲取並設置current_user。如何從其他控制器調用ApplicationController中的方法?
class ApplicationController < ActionController::Base
protected
def current_user
@current_user ||= User.find_by_id(session[:user_id])
end
def current_user=(user)
@current_user = user
session[:user_id] = user.nil? ? user : user.id
end
end
但是,當我登錄時,不調用current_user =方法。
class UsersController < ApplicationController
def sign_in
if (@mailbox = Mailbox.find_by(email: params[:email], password: params[:password]))
current_user= @mailbox.user
redirect_to mailboxes_path
else
redirect_to :root
end
end
end
另外,current_user的值在其他類中爲零。 如何確保在ApplicationController中定義的方法被調用?
啊我明白了。非常感謝你! – user1742188
如果此答案有助於解決您的問題,請考慮將其接受爲正確的:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – zeantsoi