如何实现在rails3中记住我的功能

时间:2013-02-02 20:52:48

标签: ruby-on-rails

如何实现在rails中记住我的功能。我正在使用authologic gem 我的UserSession控制器的代码是:

请建议我如何实施记住我的任何参考。我已尝试使用此代码但无法实现它。

class UserSessionsController < ApplicationController
    before_filter :require_no_user, :only => [:new, :create]
    before_filter :require_user, :only => :destroy
    def new
        @user_session = UserSession.new
    end

    def create
        @user_session = UserSession.new(params[:user_session],true)
        if @user_session.save
            flash[:notice] = "Login successful!"
            unless cookies[:last_visited].nil?
                set_last_visited
            end

            cookies.permanent[:last_visited] = Time.now
            if params[:remember_me]
                user = @user_session.user
            user.update_attributes(:remember_token => create_remember_token)
                cookies[:remember_token] = { :value => user.remember_token, :expires => 24.weeks.from_now }
            end
            redirect_back_or_default users_url(@current_user)
        else
            flash[:error]="invalid login and password"
            render :action => :new
        end
    end

    def destroy
    cookies.delete(:remember_token)
        current_user_session.destroy
        flash[:notice] = "Logout successful!"
        redirect_back_or_default home_path
    end

    private

    def create_remember_token
        SecureRandom.urlsafe_base64
    end

end

1 个答案:

答案 0 :(得分:0)

你试过这种方式吗?

UserSession.create(:login => "yourusername", :password => "yourpassword", :remember_me => true)