我非常感谢任何帮助,以确定为什么我无法退出我的示例应用程序。 *我的所有rspec测试都通过了。 *当我注册登录本地服务器时,单击注销,我没有注销,链接也没有更改为登录(虽然这是一个rspec测试通过)。
以下是authentication_pages_spec.rb
的相关测试require 'spec_helper'
describe "AuthenticationPages" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('title', text: 'Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_selector('title', text: 'Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
describe "after visiting another page" do
before {click_link "Home" }
it { should_not have_selector('div.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it {should_not have_link('Sign in', href: signin_path) }
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link ('Sign in') }
end
end
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/password combination"
render 'new'
end
end
def destroy
sign_out
redirect_to root_path
end
end
这是sessions_helper.rb
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def signed_in?
!self.current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
end
这是_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>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
最后,这是模型user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = user.email.downcase }
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
这是我的WEBrick服务器所说的:
Started DELETE "/signout" for 127.0.0.1 at 2012-06-19 10:58:53 -0400
Processing by SessionsController#destroy as HTML
Parameters: {"authenticity_token"=>"qP1xAF4YXTaFmjeHxS5SgvUx+6+c6us5AL4jqgEBeqQ="}
Redirected to http://localhost:3000/
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2012-06-19 10:58:53 -0400
Processing by StaticPagesController#home as HTML
Rendered static_pages/home.html.erb within layouts/application (0.6ms)
Rendered layouts/_shim.html.erb (0.1ms)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" IS NULL LIMIT 1
Rendered layouts/_header.html.erb (3.1ms)
Rendered layouts/_footer.html.erb (0.5ms)
Completed 200 OK in 22ms (Views: 21.2ms | ActiveRecord: 0.3ms)
[2012-06-19 10:58:53] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2012-06-19 10:58:53 -0400
Served asset /application.css - 304 Not Modified (11ms)
[2012-06-19 10:58:53] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
如果这里的cookie存在问题,那么当我在浏览器中检查cookie时会发生什么:
检查cookies: 我清除所有的饼干 2.转到localhost:3000。我仍然以用户2的身份登录。我检查了cookie,我没有本地主机的remember_token。我有一个名为“_sample_app_session”的cookie 3.单击“注销”。没有任何事情发生,仍然登录。 4.输入/登录。以用户3身份登录 我现在在cookies中有一个remember_token。 6.单击“注销”。仍然显示我已登录顶级导航。 7.我单击配置文件以查看我以用户2身份登录(通过调试) 8.我检查cookie并记住令牌消失了,我仍然有_sample_app_session
你有什么方法可以测试可能导致这种情况的原因吗?我真的想解决这个问题,所以我可以继续学习本教程。
答案 0 :(得分:5)
哦我也遇到了同样的问题。即使我也是为了拉扯我的头发。然后我的一个朋友注意到了代码并帮助了我。 问题是你错过了从RAILS CONSOLE添加记忆令牌。只需检查黄瓜上面的部分是可选的。您将注意到要从rails控制台添加令牌的部分。
rails console
User.first.remember_token
=&GT;零
User.all.each {| user | user.save(validate:false)}
User.first.remember_token
答案 1 :(得分:1)
确保在推送到heroku后迁移数据库。我遇到了类似的问题,在我跑完后它就消失了:
$ heroku run rake db:migrate