简体   繁体   English

Rails教程:Cookie与记住令牌不匹配

[英]rails tutorial: cookie doesn't match remember token

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. 我正在做迈克尔·哈特尔(Michael Hartl)的Rails教程第8章。当我尝试通过存储在浏览器cookie中的Remember_token查找用户时,它不起作用。 The find_by method returns NIL. find_by方法返回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. 我一直在尝试通过查看存储在浏览器中的记住令牌cookie并将其与存储在用户数据库中的记住令牌cookie进行调试。 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. 当我将浏览器中存储的cookie的值与数据库中存储的记住令牌的值进行比较时,它们不匹配。

Another thing I noticed is that I can't make a call to User_find_by. 我注意到的另一件事是我无法致电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. 但是,我可以致电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.html.erb代码:

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

It looks like the problem could be line 20 in your SessionsHelper. 看来问题可能出在您的SessionsHelper中的第20行。 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. 而不是将记住令牌传递到User.find_by()方法中,而是尝试调用方法find_by_remember_token,该方法不存在。

first of all, User_find_by() isn't a method. 首先, User_find_by()不是方法。 it's User.find() or User.find_by_columnName() where columnname is the column in your database that you want to search. 它是User.find()User.find_by_columnName() ,其中columnname是您要搜索的数据库中的列。

also, you have to make sure your User model is defined correctly. 另外,您必须确保正确定义了User模型。 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? 我假设你遵循直到这一点,所有的指令让你有new_remember_tokenencrypt方法,而且有create_remember_token私有方法? also make sure you have the before_create filter. 还请确保您具有before_create过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM