Hartl Rails 教程第 8 章 - 无法注销 Sample_App.所有 rspec 测试通过 [英] Hartl Rails Tutorial Chapter 8 - Cannot logout of Sample_App. All rspec tests pass

查看:54
本文介绍了Hartl Rails 教程第 8 章 - 无法注销 Sample_App.所有 rspec 测试通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您能帮助我确定我无法退出示例应用的原因,我将非常感谢.* 我所有的 rspec 测试都通过了.* 当我在本地服务器上注销时,单击注销,我没有注销,并且链接不会更改为登录(尽管为此通过了 rspec 测试).

I would be extremely thankful for any help identifying why I cannot log out of my sample app. * All of my rspec tests pass. * When I go to log out on my localserver click log out, I am not logged out, and the link does not change to Sign In (although a rspec test for this passes).

这里是来自 authentication_pages_spec.rb 的相关测试

Here are the relevant tests from 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

Here is 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

Here is 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

Here is _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> 

</标题>

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

最后,这里是模型 user.rb

Here is what my WEBrick server is saying:

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 

这是我的 WEBrick 服务器所说的:

In case it is some issue with the cookies here is what is happening when I check the cookies in the browser:

Checking the cookies:
1. I clear all cookies
2. go to localhost:3000.  I am still signed in as user 2. I check cookies and I have no remember_token for local host. i do have a cookie named "_sample_app_session"
3. click sign out. Nothing happens, still signed in.
4. enter /signin. log in as user 3
5. i now have a remember_token in cookies.
6. click sign out. still showing i am signed in the top nav.
7. I click profile to see that I am logged in as user 2 (via debug)
8. i check cookies and remember token is gone, i still have the _sample_app_session

如果 cookie 有问题,这里是我在浏览器中检查 cookie 时发生的情况:

Do you have any way I can test what might be causing this? I am really trying to solve this so I can procede with the tutorial.

检查cookies:1.我清除所有cookie2. 转到本地主机:3000.我仍然以用户 2 的身份登录.我检查了 cookie,但我没有本地主机的 remember_token.我确实有一个名为_sample_app_session"的 cookie3.点击退出.什么都没发生,仍然登录.4. 输入/登录.以用户 3 身份登录5. 我现在在 cookie 中有一个 remember_token.6.点击退出.仍然显示我已在顶部导航中签名.7. 我点击 profile 看到我是作为用户 2 登录的(通过调试)8. 我检查 cookie 并记得令牌不见了,我还有 _sample_app_session

解决方案

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

Oh I too was having the same problem. Even I was pulling my hair off for the same. Then one of my friend noticed the code and helped me out. The problem is that you just missed to add the remember token from RAILS CONSOLE. Just check the part above cucumber which is optional. You will notice the part to add tokens from rails console.

推荐答案

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

导轨控制台

User.first.remember_token

=> 无

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

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

User.first.remember_token

User.first.remember_token

这篇关于Hartl Rails 教程第 8 章 - 无法注销 Sample_App.所有 rspec 测试通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆