I am doing Michael Hartl's Rails Tutorial Chapter 8. When I try to find a user by remember_token stored in the browser cookie it isn't working. The find_by method returns NIL. I have been trying to debug by looking at the remember token cookie stored on the browser and comparing it to the remember token stored in the user database. They don't match and I don't know why. Here is the code for the Session Helper.
module SessionsHelper
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
#remember_token = User.encrypt(cookies[:remember_token])
remember_token = "71e45660fbaa69bad9fb55b912f80122a584f6af"
#@current_user ||= User.find_by(remember_token: remember_token)
@current_user ||= User.find_by_remember_token(remember_token)
end
end
I have been tweaking it to try and figure out what is going on. To debug I commented out the normal lines and set the remember token explicitly with the value I see in the database - then the app works. When I compare the value of the cookie stored in the browser to the value of remember token stored in the database they don't match.
Another thing I noticed is that I can't make a call to User_find_by. I get an error that says it doesn't recognize this method so I commented it out. I can however call to User.find_by_remember_token. It is possible that I have the wrong version of something installed?
I have tried resetting the database - but I can see it and it looks like it has all the right columns.
Here is the _header.html.erb code:
<header class="navbar navbar-fixed-top navbar-inverse">
<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", 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>