2011-12-05 8 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

私はまったく同じ問題を抱えていましたが、私のUserモデルに必要なすべての列がありませんでした。 (db/schema.rbから)

私の元Userモデルはかなり最小限だった:

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?'...エラーを取得していない:私の最後のUserモデルは次のようになります。

幸運を祈る!

(参照/詳細:http://www.michaelhamrah.com/blog/2009/05/authlogic-and-openid-on-rails/ - logged_out?ためのページ内の検索は、説明が3/4程度ポストダウン道のでした。)

+1

は唯一の「last_request_at」、「logged_out」の未定義のメソッドを解決するために必要です。 –

2

ユーザーがログアウトしているかどうかを知りたい場合は、あなたが行うことができます:

if current_user_session 
    ... 

、彼らがしている場合、この条件はfalseユーザーがログインしている場合(セッションがある)trueを返す、となりますログアウトしました(セッションはnil)。

エラーメッセージの場合、undefined method 'logged_out?' for #<User:0x00000103ee8348>は、logged_out?というメソッドを定義していないことを意味しています。

AuthlogicはUserモデルのlogged_out?メソッドを定義しておらず、あなたもいないので、何も呼び出すことはありません。その理由は、「ログイン中」または「ログアウト中」のいずれかの状態がUserモデルとは関係なく、特定のユーザーがアクティブなUserSessionレコードを持っているかどうかのプロパティです。

関連する問題