Jea*_*ean 1 ruby ruby-on-rails ruby-on-rails-3.2
嗨,我正在进行重置密码操作.但点击按钮后我收到此错误:
被称为nil的id,错误地为4 - 如果你真的想要id为nil,请使用object_id
这是我的password_reset_controller
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
Run Code Online (Sandbox Code Playgroud)
这是我的用户模型
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
Run Code Online (Sandbox Code Playgroud)
这是观点:
<% 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>
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我找不到用户; 我尝试在rails控制台上做同样的事情,我可以通过电子邮件找到用户,但我可以生成password_reset_token.
我感谢你的帮助.
谢谢
使用params [:password_resets] [:email]
请看User.all,看看.检查您调用password_reset_token方法的用户记录
这意味着您的数据库中没有此电子邮件的用户.
使用,
user = User.find_by_email!(params[:email])
Run Code Online (Sandbox Code Playgroud)
带爆炸(!)的方法会触发异常.find_by_email如果找不到电子邮件,则返回nil对象
| 归档时间: |
|
| 查看次数: |
3368 次 |
| 最近记录: |