1
我在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
感謝您的幫助 – skip87