2013-06-20 64 views

回答

1

如果用戶離開網站,或關閉瀏覽器等,並且希望他們能夠保持登錄狀態:

在你SessionsController:

def create 
    member = Member.find_by_user_name(params[:user_name]) 
    if member && member.authenticate(params[:password]) 
     session[:member_id] = member.id 
     if params["remember_me"] == "1" 
     cookies[:digest] = {:value => member.password_digest, :expires => Time.now + 720000} 
     else 
     cookies[:digest] = nil 
     end 
     redirect_to (url_to_go_to_after_login), :notice => "Logged in!" 
    else 
     redirect_to (login_url), :alert => "Invalid email or password" 
    end 
    end 

如何註銷用戶:

def destroy 
    session[:member_id] = nil 
    cookies[:digest] = nil 
    redirect_to url_to_go_to_after_logout, :notice => "Logged out!" 
    end 

如何從rememberme coo登錄用戶紀伊:

def new 
    if member = Member.find_by_password_digest(cookies[:digest]) 
     session[:member_id] = member.id 
     redirect_to (url_to_go_to_after_login), :notice => "Hello#{member.first_name}" 
    end 
    end 

要設置過期時間後(應該工作在軌> 2.3 https://github.com/rails/rails/blob/2-3-stable/actionpack/lib/action_controller/session/abstract_store.rb#L175):

Your::Application.config.session_store :active_record_store, { 
    key: "your_session_id", 
    domain: ".your-domain.com", 
    expire_after: 48.hours, 
} 
+0

感謝馬克斯您的反饋,這是一個完整的解決方案,我只需要在到期時間幫助你已經在上面添加了。 – jmrp

相關問題