我在 Michael Hartl 教程第 8 章登录和注销。我已经成功登录了。但是注销不起作用。我在尝试注销后保持登录状态。注销后测试登录按钮时的 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