Rails Auth,记住我

我正在尝试按照Ryan Bates的教程添加记住我的登录表单function。 但是,当我尝试访问我的页面时,我收到此错误:

找不到具有auth_token =用户

我认为问题来自我的application_controller.rb

 class ApplicationController < ActionController::Base protect_from_forgery helper_method :current_user private def current_user @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token] end end 

这是我的user.rb中的代码

 class User  :create before_create { generate_token(:auth_token) } def send_password_reset generate_token(:password_reset_token) self.password_reset_sent_at = Time.zone.now save! UserMailer.password_reset(self).deliver end def generate_token(column) begin self[column] = SecureRandom.urlsafe_base64 end while User.exists?(column => self[column]) end end 

这是我的user_controller.rb

 class UsersController  "Signed up!" else render "new" end end end 

最后这是我的sessions_controller

 class SessionsController  "Logged in!" else flash.now.alert = "Invalid email or password" render "new" end end def destroy cookies.delete(:auth_token) redirect_to root_url, :notice => "Logged out!" end end 

我唯一改变的是通过用户名而不是电子邮件找到用户,但我不明白为什么会破坏它。 有人有任何想法吗?

编辑

我的数据库由以下迁移填充

 class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :username t.string :email t.string :password_digest t.timestamps end end end class AddAuthTokenToUsers < ActiveRecord::Migration def change add_column :users, :auth_token, :string end end class AddPasswordResetToUsers < ActiveRecord::Migration def change add_column :users, :password_reset_token, :string add_column :users, :password_reset_sent_at, :datetime end end end 

这是根据终端窗口的错误:

 ActionView::Template::Error (Couldn't find User with auth_token = ): 1: 

Home

2: 3:
4: 5: Logged in as 6: 7: app/controllers/application_controller.rb:8:in `current_user' app/views/pages/home.html.erb:4:in `_app_views_pages_home_html_erb___725535246_2189699680'

编辑2

这是home.html.erb文件的代码

 

Home

Logged in as or

这可能不是答案,但你尝试过这样的事情:

 

Home

<% if !current_user.auth_token.nil? %> Logged in as <%= current_user.email %> <%= link_to "Log out", logout_path %> <% else %> <%= link_to "Sign up", signup_path %> or <%= link_to "Log in", login_path %> <% end %>

我怀疑,由于某种原因,current_user的auth_token为null。 您也可以尝试将逻辑修改为:

 
cu nil?:<%=current_user.nil?%> cu.at.nil?:<%=current_user.auth_token.nil? if !current_user.?nil%> <%= link_to "Log out", logout_path %> <%= link_to "Sign up", signup_path %> or <%= link_to "Log in", login_path %>

…在调试此问题时。 至少这应该可以为您提供更多信息,以及在您进一步挖掘时加载的页面。

我遇到过同样的问题。 在将auth_token字段添加到用户数据库表之前,问题仅发生在我在数据库中的用户身上。 应用程序Couldn't find User with auth_token因为旧用户在注册时没有为他们创建用户,因为此function尚未实现。

为了解决这个问题,我只是从我的数据库中删除了旧用户并重新签名。

但是不再自动登录的用户。 为了解决这个问题,我更改了这一行:

session[:user_id] = @user.id

cookies[:auth_token] = @user.auth_token

因为我现在使用cookie而不是会话来签署用户。

我遇到了同样的问题,在阅读了所有这些post和RailsCasts的想法之后,我尝试更新应用程序控制器,检查从cookie返回的auth_token是否实际包含了某些内容并开始工作。

 def current_user @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token] && cookies[:auth_token].length > 0 end 

我不确定cookies [:auth_token]是否返回nil并且条件没有捕获它或者如果cookies [:auth_token]实际上返回一个空字符串并且User.find_by_auth_token不喜欢它,即使它工作了在控制台中。

不确定这是否是正确的方式,但我想我会把它扔出去。

谢谢Patrick