2015-04-03 110 views
1

我目前的想法是重寫會話create方法,has_many關聯到user_logins模型並記錄所有會話創建。雖然,我不確定如何去做。跟蹤所有設計登錄的最佳方式是什麼?

def create 
    self.resource = warden.authenticate!(auth_options) 
    set_flash_message(:notice, :signed_in) if is_flashing_format? 
    sign_in(resource_name, resource) 
    yield resource if block_given? 
    respond_with resource, location: after_sign_in_path_for(resource) 
    end 

難道我只是在滑User_Logins.create(:date => current_sign_in_at, :ip => current_sign_in_ip) - 該sign_in(resource_name, resource)方法被調用後,想必使當前屬性已被更新。

任何想法都會很棒,謝謝。

回答

0

設計提供了一個方法調用,您可以用在成功註冊後執行一些代碼。

class ApplicationController < ActionController::Base 
    def after_sign_in_path_for(resource) 
    UserLogin.create(:date => Date.today, :ip => request.remote_ip, :user_id => current_user.id) 
    end 
end 

請注意,我已經叫模式「用戶登陸」,這將默認映射到表名爲「 user_logins「,這是更常規的配置。

+0

這就是定義重定向到登錄後的url,而不是回調添加日誌記錄等。它會在技術上工作,我猜想,但相當hackish。 – errata 2015-04-03 16:21:25

+0

這些怎麼樣? http://stackoverflow.com/questions/10500653/an-efficient-way-to-track-user-login-dates-and-ips-history – fatfrog 2015-05-12 23:18:53

0

你想每個登錄或更好的使用統計數據?會話將保持活躍,如果有人在幾個小時後回來,他們可能已經登錄。我建議使用真正的分析可能以js客戶端(GA,mixpanel,analytics.js)的形式或添加默認的日誌記錄操作到application_controller.rb。如果你真的只想添加相當於會話創建的「登錄」日誌記錄,請在session.rb回調之一中添加一個日誌記錄操作。

相關問題