2011-12-06 116 views
0

我正在學習創建用戶登錄和註冊。 這裏是註冊我的HTML代碼,Ruby on Rails 3:與會話玩

<% if signed_in? %> 
    <li><%= link_to "Sign out", signout_path, :method => :delete %></li> 
<% else %> 
    <li><%= link_to "Sign in", signin_path %></li> 
<% end %> 

但是,當我在成功的跡象,Sign out顯示不出來。

有人幫助我嗎? :d

這是我在SessionHelper

signed_in?代碼

更新1

module SessionsHelper 
    def sign_in(user) 
    cookies.permanent.signed[:remember_token] = [user.id, user.salt] 
    self.current_user = user 
    end 
    def current_user=(user) 
    @current_user = user 
    end 
    def current_user 
    @current_user ||= user_from_remember_token 
    end 
    def signed_in? 
    !current_user.nil? 
    end 
    def sign_out 
    cookies.delete(:remember_token) 
    self.current_user = nil 
    end 
    def deny_access 
    redirect_to signin_path, :notice => "Please sign in to access this page." 
    end 
    def current_user?(user) 
    user == current_user 
    end 
    def redirect_back_or(default) 
    redirect_to(session[:return_to] || default) 
    clear_return_to 
    end 

private 
    def user_from_remember_token 
    User.authenticate_with_salt(*remember_token) 
    end 
    def remember_token 
    cookies.signed[:remembered_token] || [nil, nil] 
    end 
    def store_location 
    session[:return_to] = request.fullpath 
    end 
    def clear_return_to 
    session[:return_to] = nil 
    end 
end 

這是我的SessionController

更新2

class SessionsController < ApplicationController 
def new 
    @title = "Sign In" 
end 
def create 
    user = User.authenticate(params[:session][:email], params[:session][:password]) 
    if user.nil? 
    flash.now[:error] = "Invalid email/password combination." 
    @title = "Sign in" 
    render 'new' 
else 
    sign_in user 
    redirect_to user 
end 
end 
def destroy 
    sign_out 
    redirect_to root_path 
end 
end 
+1

請問您可以發佈'signed_in?'方法的代碼嗎? – stephenmurdoch

+0

當我登錄時,瀏覽器將我帶到用戶配置文件。 但是'sign out'菜單沒有顯示出來替換'sign in'。 請參閱SessionsController的#update 2 – mamatozay

回答

0

在SessionsHelper,更改

def remember_token 
    cookies.signed[:remembered_token] || [nil, nil] 
end 

def remember_token 
    cookies.signed[:remember_token] || [nil, nil] 
end 

非常感謝您的支持。 :D