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: