2011-02-22 26 views
4

HI,創建會話,而用戶註冊在軌

我在Rails應用程序的註冊功能的一些疑惑(2.3.10)

在我的意見/用戶/ new.html.erb

<div id="signup_form" style="display:none;" class="login-drpdwn "> 
    <% form_for :user, :url => users_path do |f| -%> 
    <%= error_messages_for 'user' %><br/> 
    <div> 
     <p> Enter your name </p> 
     <%= f.text_field :name, :class => "text_box" %> 
     <p> Enter your login name </p> 
     <%= f.text_field :login, :class => "text_box" %> 
     <p> Email ID</p> 
     <%= f.text_field :email, :class => "text_box" %> 
     <p> Password</p> 
     <%= f.password_field :password, :class => "text_box" %> 
     <p> Confirm your password</p> 
     <%= f.password_field :password_confirmation, :class => "text_box" %> 
     <div class="textalign_right"> 
     <input type="submit" value="Submit" class="submit_button rounded_corner"/> 
     </div> 
    </div> 
    <% end %> 
</div> 

在我的用戶控制

before_filter :login_required, :except => [:new, :create, :forgot, :reset, :process_forgot_password, :activate] 

def new 
    @user = User.new 
end 

def create 
    @user = User.new(params[:user])  
    if @user.save 
    redirect_to "/blogs/home",:notice => "signed up" 
    else 
    redirect_to "new" 
    end 
end 

在我的用戶模型

before_save :encrypt_password 
attr_protected :id, :password_salt 
attr_accessor :password, :password_confirmation 

def self.authenticate(email, password) 
    user = find_by_email(email) 
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt) 
    user 
    else 
    nil 
    end 
end 

def encrypt_password 
    if password.present? 
    self.password_salt = BCrypt::Engine.generate_salt 
    self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) 
    end 
end 

在我的會話控制器 - 創建方法

def create 
    @current_user = User.find_by_login(params[:login].downcase) 
    if @current_user.blank? || params[:password] != @current_user.password_hash 
    @current_user = nil 
    render :action => "new" 
    else 
    session[:user_id] = @current_user.id 
    session[:close_time] = 1800.seconds.from_now 

    if @current_user.last_login.nil? 
     @login = LastLogin.new 
     @login.user_id = @current_user.id 
     @login.login_at = Time.now 
     @login.save 
    else 
     @login = @current_user.last_login 
     @login.last_at = @login.login_at 
     @login.login_at = Time.now 
     @login.login_count += 1 
     @login.save 
    end 

    if session[:return_to] 
     redirect_to session[:return_to], :protocol => USE_PROTOCOL 
     session[:return_to] = nil 
    else 
    end 
    end 
end 

當我提交的註冊用戶是越來越創建的頁面,但不是會話 所以它不是引導至博客/主頁,因爲它有的before_filter:login_required有在博客控制器..

如何解決這個 我的代碼是不是有包括身份驗證系統線

回答

4
 
    session[:id] = @current_user.id 
+0

我應該在哪裏插入該行?/ – useranon

+0

在Create方法中修改第7行。所以一般來說,使用預定義的會話[:id]而不是session [:user_id] –