Ruby on Rails 3:玩会话

时间:2011-12-06 02:44:54

标签: ruby-on-rails ruby-on-rails-3

我正在学习创建用户登录和注册。 这是我登录的HTML代码,

<% 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 个答案:

答案 0 :(得分:0)

在SessionsHelper中,更改

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

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

非常感谢您的支持。 :d