2011-12-05 40 views
0

這是我的設置:Authlogic - 如何檢查,如果用戶登錄了

user.rb

acts_as_authentic do |c| 
    c.logged_in_timeout(1.minutes) 
    end 

user_session.rb

def to_key 
    new_record? ? nil : [ self.send(self.class.primary_key) ] 
    end 
    self.logout_on_timeout = true 

application_controller.rb

helper_method :current_user_session, :current_user 

    private 
    def current_user_session 
     logger.debug "ApplicationController::current_user_session" 
     return @current_user_session if defined?(@current_user_session) 
     @current_user_session = UserSession.find 
    end 

    def current_user 
     logger.debug "ApplicationController::current_user" 
     return @current_user if defined?(@current_user) 
     @current_user = current_user_session && current_user_session.user 
    end 

    def require_user 
     logger.debug "ApplicationController::require_user" 
     unless current_user 
     #store_location 
     flash[:warning] = "You must be logged in to access this page" 
     #redirect_to new_user_session_url 
     redirect_to root_url 
     return false 
     end 
    end 

    def require_no_user 
     logger.debug "ApplicationController::require_no_user" 
     if current_user 
     #store_location 
     flash[:warning] = "You must be logged out to access this page" 
     redirect_to account_url 
     return false 
     end 
    end 

但是當我加載我的網頁時,我會得到的錯誤

undefined method `logged_out?' for #<User:0x00000103ee8348> 

我嘗試讀取Authlogic的官方頁面GitHub的,但我仍然不知道,我錯過......誰能給我修復它小費了嗎?

非常感謝提前!

回答

3

我有完全相同的問題,它歸結爲我沒有在我的用戶模型中所有必要的列的事實。

我原來的用戶模型(從db/schema.rb)是相當簡約:

create_table "users", :force => true do |t| 
    t.string "username" 
    t.string "name" 
    t.string "crypted_password" 
    t.string "password_salt" 
    t.string "persistence_token" 
    t.string "perm",    :default => "employee" 
end 

不過,我加列t.datetime :last_requested_at到我的模型,以及可能會或可能不會需要幾個人。我的最終用戶模型的樣子:

create_table "users", :force => true do |t| 
    t.string "username" 
    t.string "name" 
    t.string "crypted_password" 
    t.string "password_salt" 
    t.string "persistence_token" 
    t.string "perm",    :default => "employee" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "login_count",  :default => 0,   :null => false 
    t.integer "failed_login_count", :default => 0,   :null => false 
    t.datetime "last_request_at" 
    t.datetime "current_login_at" 
    t.datetime "last_login_at" 
    t.string "current_login_ip" 
    t.string "last_login_ip" 
end 

在其他列加入後,我不再得到undefined method 'logged_out?'...錯誤。

祝你好運!

(參考/詳細信息:http://www.michaelhamrah.com/blog/2009/05/authlogic-and-openid-on-rails/ - 在網頁搜索logged_out?,說明了約3/4的一路下滑後)。

+1

爲了解決未定義的方法「logged_out」,只有「last_request_at」是必要的。 –

2

如果你想知道,如果一個用戶登錄時,你可以這樣做:

if current_user_session 
    ... 

這種情況將返回true如果用戶在(有一個會話)登錄,並false如果他們註銷(會話是nil)。

至於錯誤消息,undefined method 'logged_out?' for #<User:0x00000103ee8348>意味着你還沒有定義一個名爲logged_out?的方法,所以它不知道你的意思。

Authlogic沒有爲User模型定義logged_out?方法,也沒有你,所以沒有什麼可以調用的。原因是「登錄」或「退出」狀態與User型號沒有任何關係,而是一個給定用戶是否具有活動的UserSession記錄的屬性。