Michael Hartl Rails Tut 3 Ch 8 - 注销无法呈现正确的视图

时间:2014-11-11 11:01:47

标签: ruby-on-rails ruby-on-rails-4 railstutorial.org

我已经思考了很长时间了,真的无法弄清楚这个问题,希望这里的专业人员在堆栈溢出时可以启发我!

我正在做Michael Hartl Rails教程第8章,目前停留在8.3,我已经完成了该部分,但是当我退出时,我的标题仍然呈现登录标题视图,这对if / else没有意义我的_header.html.erb代码中的声明。

这些是我的_header.html.erb代码,这只是教程中的纯文本复制和粘贴,我很确定我已经注销,因为我的集成测试已经过去了。

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="container">
    <%= link_to "sample app", root_path, id: "logo" %>
    <nav>
      <ul class="nav navbar-nav pull-right">
        <li><%= link_to "Home", root_path %></li>
        <li><%= link_to "Help", help_path %></li>
        <% if logged_in? %>
          <li><%= link_to "Users", '#' %></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
              Account <b class="caret"></b>
            </a>
            <ul class="dropdown-menu">
              <li><%= link_to "Profile", current_user %></li>
              <li><%= link_to "Settings", '#' %></li>
              <li class="divider"></li>
              <li>
                <%= link_to "Log out", logout_path, method: "delete" %>
              </li>
            </ul>
          </li>
        <% elsif %>
          <li><%= link_to "Log in", login_path %></li>
        <% end %>

        </ul>
    </nav>
  </div>
</header>

我的测试如下:

  test "login with valid information followed by logout" do
    get login_path
    post login_path, session: { email: @user.email, password: 'password' }
    assert is_logged_in?
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
    delete logout_path
    assert_not is_logged_in?
    assert_redirected_to root_url
    follow_redirect!
    assert_select "a[href=?]", login_path <-- line 33
    assert_select "a[href=?]", logout_path,      count: 0
    assert_select "a[href=?]", user_path(@user), count: 0
  end

我的注销方法在我的帮助文件中定义如下:

  def log_out
    session.delete(:user_id)
    @current_user = nil
  end

我的destroy方法在我的控制器文件中定义如下:

  def destroy
    log_out
    redirect_to root_url    
  end
end

最后错误信息如下,

 FAIL["test_login_with_valid_information_followed_by_logout", UsersLoginTest, 0.341018]
 test_login_with_valid_information_followed_by_logout#UsersLoginTest (0.34s)
        Expected at least 1 element matching "a[href='/login']", found 0.
        Expected 0 to be >= 1.
        test/integration/users_login_test.rb:33:in `block in <class:UsersLoginTest>'

  20/20: [=================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.64770s
20 tests, 52 assertions, 1 failures, 0 errors, 0 skips

[1] guard(main)> 

感谢有人能真正向我解释!

3 个答案:

答案 0 :(得分:0)

<强> _header.html.erb

变化:

<% elsif %>
  <li><%= link_to "Log in", login_path %></li>
<% end %>

要:

<% else %>
  <li><%= link_to "Log in", login_path %></li>
<% end %>

答案 1 :(得分:0)

我遇到了同样的问题。得到了同样的错误,并且失败了同样的测试。原来我把

  # Forgets a persistent session. 
  def forget(user)
    user.forget
    cookies.delete(:user_id)
    cookies.delete(:remember_token)
  end

  # Logs out the current user.
  def log_out
    forget(current_user)
    session.delete(:user_id)
    @current_user = nil
  end

app/models/user.rb而不是app/helpers/sessions_helper.rb和方法

  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

应该在app/models/user.rb但不在那里。

所以我有一个典型的复制/粘贴愚蠢的错误。希望它有所帮助

答案 2 :(得分:0)

我有同样的问题,我忘了包括

forget(current_user)

在app / helpers / sessions_helper.rb

中的方法def log_out