gpt4 book ai didi

ruby-on-rails - Hartl Rails 教程第 8 章 - 无法注销 Sample_App。所有 rspec 测试都通过

转载 作者:行者123 更新时间:2023-12-03 22:40:19 24 4
gpt4 key购买 nike

如果您能帮助我确定无法退出示例应用程序的原因,我将不胜感激。* 我所有的 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

这里是session_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

这里是session_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 时发生的情况:

检查 cookie:1.我清除所有cookies2. 转到本地主机:3000。我仍然以用户 2 的身份登录。我检查了 cookie,但我没有本地主机的 remember_token。我有一个名为“_sample_app_session”的 cookie3. 点击退出。没有任何反应,仍然登录。4. 输入/signin。以用户 3 身份登录5. 我现在在 cookie 中有一个 remember_token。6. 点击退出。仍然显示我已在顶部导航中签名。7. 我单击配置文件以查看我以用户 2 身份登录(通过调试)8. 我检查 cookie 并记得 token 不见了,我还有 _sample_app_session

您有什么方法可以测试可能导致此问题的原因吗?我真的很想解决这个问题,所以我可以继续学习教程。

最佳答案

哦,我也遇到了同样的问题。甚至我都在拔头发。然后我的一个 friend 注意到了代码并帮助了我。问题是您只是错过了从 RAILS CONSOLE 添加记住 token 。只需检查 cucumber 上方的可选部分即可。您会注意到从 Rails 控制台添加 token 的部分。

rails 控制台

User.first.remember_token

=> 无

User.all.each { |user| user.save(validate: false) }

User.first.remember_token

关于ruby-on-rails - Hartl Rails 教程第 8 章 - 无法注销 Sample_App。所有 rspec 测试都通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11093240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com