忘记密码rails 4.1
Posted
技术标签:
【中文标题】忘记密码rails 4.1【英文标题】:forgotten_password rails 4.1 【发布时间】:2014-08-05 16:55:06 【问题描述】:我遵循 railscast 指南 Railscast
我在提交电子邮件以更改密码时收到错误消息;
用于#的未定义方法`generate_token'
指向这条线:$generate_token(:password_reset_token)
这是我的用户模型
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
has_many :messages
before_save self.email = email.downcase
before_create :create_remember_token
validates :name, presence: true, length: maximum: 50
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
validates :email, presence: true, format: with: VALID_EMAIL_REGEX ,
uniqueness: case_sensitive: false
has_secure_password
validates :password, length: minimum: 6
#TEST
#before_create generate_token(:auth_token)
#before_create :generate_token
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
#kommer att skicka ett mail till användaren
UserMailer.password_reset(self).deliver
end
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.digest(token)
Digest::SHA1.hexdigest(token.to_s)
end
def feed
Micropost.from_users_followed_by(self)
end
def following?(other_user)
relationships.find_by(followed_id: other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by(followed_id: other_user.id).destroy
end
private
def create_remember_token
self.remember_token = User.digest(User.new_remember_token)
end
end
对我可能添加或遗漏的内容有什么建议吗?
【问题讨论】:
【参考方案1】:您似乎缺少 generate_token 方法。
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
查看视频下方的代码。
【讨论】:
感谢愚蠢的错误。但是我现在又遇到另一个错误验证失败:密码太短(最少 6 个字符)并且它指向“保存!”有什么建议吗? 使用相关代码创建一个新问题:控制器、请求和模型验证。以上是关于忘记密码rails 4.1的主要内容,如果未能解决你的问题,请参考以下文章
威联通(QNAP)NAS忘记密码怎么办?手把手教你!请收藏!关键时刻有用!