سؤال

I'm setting up a reset password feature. Upon submission an email will be sent with a reset password link. I'm doing this from scratch as described in railscast episode 274. However, when I submit my email I get this error.

NoMethodError in PasswordResetsController#create

undefined method `password_reset_sent_at=' for # <User:0xa9fd828>

app/models/user.rb:26:in `send_password_reset'
app/controllers/password_resets_controller.rb:7:in `create'

Request

Parameters:

{"utf8"=>"✓",
"authenticity_token"=>"HnuB/J15XZtz3MTCd25MCwm06M3G2AbcRH+cFxzdj+8=",
"email"=>"cauterise@gmail.com",
"commit"=>"Reset Password"}

The development log says the same thing as above ^

app/models/user.rb

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

app/controllers/password_resets_controller.rb

class PasswordResetsController < ApplicationController
  def new
  end

def create
  user = User.find_by_email(params[:email])
  user.send_password_reset if user
  redirect_to root_url, :notice => "Email sent with password reset instructions."
end

def edit
  @user = User.find_by_password_reset_token!(params[:id])
end

def update
  @user = User.find_by_password_reset_token!(params[:id])
  if @user.password_reset_sent_at < 2.hours.ago
    redirect_to new_password_reset_path, :alert => "Password reset has expired."
  elsif @user.update_attributes(params[:user])
    redirect_to root_url, :notice => "Password has been reset!"
  else
    render :edit
  end
end
end

app/controllers/application_controller.rb

def current_user
  @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]

app/vies/password_resets/new,html.haml

%h1 Reset Password
= form_tag password_resets_path, :method => :post do
  .field
    = label_tag :email
    = text_field_tag :email, params[:email]
  .actions
    = submit_tag "Reset Password"
  end

I ALREADY DID rake:db:reset and rake:db:migrate AND RESET SERVER.

EDIT: db USER

  • id integer primary key

  • email varchar(255)

  • password_hash varchar(255)

  • password_salt varchar(255)

  • created_at datetime

  • updated_at datetime

  • auth_token varchar(255)

  • password_reset_token varchar(255)

  • password_reset_send_at datetime

هل كانت مفيدة؟

المحلول

It seems that the your database users column password_reset_send_at should be password_reset_sent_at. You can change this by going into the migration files and do that simple change then running

$ rake db:reset
$ rake db:migrate
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top