rails tutorial: cookie doesn't match remember

2019-08-08 10:07发布

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>

2条回答
来,给爷笑一个
2楼-- · 2019-08-08 10:45

first of all, User_find_by() isn't a method. it's User.find() or User.find_by_columnName() where columnname is the column in your database that you want to search.

also, you have to make sure your User model is defined correctly. i'm assuming you followed all the instructions til that point so you have the new_remember_token and encrypt methods and you have the create_remember_token private method? also make sure you have the before_create filter.

查看更多
小情绪 Triste *
3楼-- · 2019-08-08 10:52

It looks like the problem could be line 20 in your SessionsHelper. Should be:

@current_user ||= User.find_by(remember_token: remember_token)

Instead of passing the remember token into the User.find_by() method you're attempting to call a method find_by_remember_token, which doesn't exist.

查看更多
登录 后发表回答