我正在使用Devise on Rails 4.1 我的問題是關於助手及其與會話的關係。 current_user:告訴您是否有用戶會話可供用戶使用。 user_signed_in:告訴你用戶是否被認證。devise rails current_user vs user_signed_in?
我不明白如果user_signed_in可以有一個current_user?是假的?
這兩種方法有什麼區別,它與會話有什麼關係。
THanks。 Richard Madson
我正在使用Devise on Rails 4.1 我的問題是關於助手及其與會話的關係。 current_user:告訴您是否有用戶會話可供用戶使用。 user_signed_in:告訴你用戶是否被認證。devise rails current_user vs user_signed_in?
我不明白如果user_signed_in可以有一個current_user?是假的?
這兩種方法有什麼區別,它與會話有什麼關係。
THanks。 Richard Madson
current_user方法返回當前登錄的用戶,而user_signed_in?方法用於驗證任何用戶是否已登錄並返回true或false。如果user_signed_in?爲false,則current_user方法將返回nil。
https://github.com/plataformatec/devise#controller-filters-and-helpers
user_signed_in?
被提供爲方便。如果user_signed_in?
是false
,那麼您的說法是正確的,因此永遠不會有current_user
。
在devise source code,我們可以看到:
def #{mapping}_signed_in?
!!current_#{mapping}
end
(其中user
採取的#{mapping}
的地方)
user_signed_in?
只是返回的current_user
的感實性,即,如果false
是current_user
nil
。