Michael Hartl Rails Tut 3 Ch 8 - logging out does not render the correct view

995 Views Asked by At

I've pondered upon this for a very long time already and really couldnt figure out the problem, hope the pros here in stack overflow can enlighten me!

Im doing Michael Hartl Rails tutorial Ch 8, currently stuck at 8.3, I've completed the section but as I log out, my header still renders the logged in header view, which doesn't make sense from the if/else statement inside my _header.html.erb codes.

These are my _header.html.erb codes, which is just a pure copy and paste from the tutorial, I'm pretty sure I've logged out as my integration test has passed.

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

my test is as follow:

  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

and my log out method is defined as follow in my helper file:

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

and my destroy method is defined as follow in my controller file:

  def destroy
    log_out
    redirect_to root_url    
  end
end

Lastly the error message is as follow,

 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)> 

Appreciate if someone can really explain it to me!

4

There are 4 best solutions below

0
On BEST ANSWER

_header.html.erb

Change:

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

To:

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

I had the same problem. Got the same error, and failed the same test. It turned out that I put the

  # 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

in the app/models/user.rb instead of app/helpers/sessions_helper.rb and the method

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

which should be in the app/models/user.rb but was not there.

So I had a typical copy/paste stupid mistake. Hope it helps

0
On

I had the same exact problem and I just fixed it. (This is my 1st post by the way so forgive me if I fudge up some etiquette. Sorry)

The issue was in app/_header.html.erb where I had:

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

Instead of:

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

I hope that helps.

0
On

I had the same problem, I forgot to include

forget(current_user)

in the method def log_out inside app/helpers/sessions_helper.rb