2

我将非常感谢任何帮助确定我无法退出示例应用程序的原因。* 我所有的 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. 我清除所有 cookie 2. 转到 localhost:3000。我仍然以用户 2 的身份登录。我检查了 cookie,但我没有本地主机的 remember_token。我确实有一个名为“_sample_app_session”的 cookie 3. 单击退出。没有任何反应,仍然登录。 4. 输入 /signin。以用户 3 身份登录 5. 我现在在 cookie 中有一个 remember_token。6. 点击退出。仍然显示我已在顶部导航中签名。7. 我单击配置文件以查看我以用户 2 身份登录(通过调试) 8. 我检查 cookie 并记住令牌已消失,我仍然拥有 _sample_app_session

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

4

2 回答 2

5

哦,我也遇到了同样的问题。甚至我也在拉我的头发。然后我的一个朋友注意到了代码并帮助了我。问题是您刚刚错过了从 RAILS CONSOLE 添加记住令牌。只需检查黄瓜上方的部分,这是可选的。您会注意到从 rails 控制台添加令牌的部分。

导轨控制台

User.first.remember_token

=> 无

User.all.each { |用户| 用户保存(验证:假)}

User.first.remember_token

于 2012-06-30T05:01:24.027 回答
1

确保在推送到 heroku 后迁移数据库。我遇到了类似的问题,运行后它就消失了:

$ heroku run rake db:migrate
于 2015-02-13T13:07:53.257 回答