嗨,我正在执行重置密码操作。但是单击按钮后,我收到此错误:
为 nil 调用 id,它会错误地为 4 - 如果你真的想要 nil 的 id,请使用 object_id
这是我的密码重置控制器
class PasswordResetsController < ApplicationController
layout "sessions"
def new
end
def create
user = User.find_by_email(params[:email])
user.send_password_reset if user
redirect_to root_url, :notice => "#{user.id}Las instrucciones para reestrablecer la contrasena fueron enviadas."
end
end
这是我的用户模型
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
def send_password_reset
self.password_reset_token = SecureRandom.urlsafe_base64
self.password_reset_at = Time.zone.now
save!
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
这是视图:
<% provide(:title, "Reiniciar Password") %>
<div class="row">
<div class="span4">
 
</div>
<div class="span4" id="login-box">
<div id="login-controls">
<%= link_to(image_tag("logo.png"), root_path) %>
<br>
<br>
<%= form_for(:password_resets, url: password_resets_path) do |f| %>
<%= f.text_field :email, :placeholder => "Correo Electronico", :tabindex => 1, :style => "height:25px;" %>
<%= f.button "<i class=\"icon-lock icon-white\"></i> Reiniciar Password".html_safe, :tabindex => 2, class: "btn btn-warning", :style => "width:220px;margin-bottom:5px;" %>
<% end %>
</div>
</div>
<div class="span4">
</div>
</div>
我不明白为什么找不到用户;我尝试在 Rails 控制台上做同样的事情,我可以通过电子邮件找到用户,但我可以生成 password_reset_token。
请感谢您的帮助。
谢谢