在我的会话中注销时出现问题

时间:2012-11-21 01:53:08

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

我在Michael Hartl教程第8章登录和注销。我已成功登录。但注销不起作用。我在尝试注销后仍然保持登录状态。 在loggout之后测试登录按钮时的rspec没有捕获错误  这是代码。对

视图/布局/ header.html.erb

<header class="navbar navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
           <%= link_to "sample app", root_path , id: "logo"%>
             <nav>
                <ul class="nav pull-right">
                    <li><%= link_to "Home", root_path %></li>
                    <li><%= link_to "Help", help_path %></li>
                    <% if signed_in? %>
                    <li><%= link_to "Users", '#' %></li>
                    <li id="fat-menu" class="dropdown">
                       <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                          Account <b class="caret"></b>
                       </a>
                         <ul class= "dropdown-menu">
                           <li><%= link_to "Profile", user_path(current_user) %></li>
                           <li><%= link_to "Settings", '#' %></li>
                           <li class="divider"></li>
                           <li>
                            <%= link_to "Sign out", signout_path, method: "delete" %>
                           </li>
                        </ul >

                    <% else %>
                    <li><%= link_to "Sign in", signin_path %></li>
                    <% end %>
                </ul>
             </nav>
        </div>
    </div>
</header>

助手/ sessions_helper.rb

module SessionsHelper

   def sign_in(user)
     cookies.permanent[:remember_token] = user.remember_token
     current_user = user
   end

   def signed_in?
     !current_user.nil?
   end

   def curent_user=(user)
     @current_user = user
   end

   def current_user
     #@current_user = @current_user || User.find_by_remember_token(cookies[:remember_token])
     # same thing 
      @current_user ||=  User.find_by_remember_token(cookies[:remember_token])
   end

   def sign_out
      current_user = nil
      cookies.delete(:remember_token)
   end
end

控制器/ sessions_controller.rb

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by_email(params[:session][:email])
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_to user
    else
     flash.now[:error] = "Invalid email or password combination"
         render 'new'
    end
  end

  def destroy
    sign_out
    redirect_to root_path
  end
end

1 个答案:

答案 0 :(得分:1)

最终使用rake db:reset