Topic: Devise and Password Resets
I have an application that has devise installed. The password reset code is already written but I'm getting an error whenever I try to actually use the pw reset feature. I would like to user recoverable.rb inside of devise as my password recovery code but just need a little help setting this up. am I missing a require somewhere? do I need to add some table columns? any help much appreciated I really need to get this pw reset tool finished ![]()
NoMethodError (undefined method `reset_password_token=' for #<User:0x00000003338850>):
activerecord (2.3.10) lib/active_record/attribute_methods.rb:255:in `method_missing'
vendor/plugins/devise/lib/devise/models/recoverable.rb:42:in `generate_reset_password_token'
vendor/plugins/devise/lib/devise/models/recoverable.rb:48:in `generate_reset_password_token!'
vendor/plugins/devise/lib/devise/models/recoverable.rb:34:in `send_reset_password_instructions'
vendor/plugins/devise/lib/devise/models/recoverable.rb:63:in `send_reset_password_instructions'
vendor/plugins/devise/app/controllers/passwords_controller.rb:13:in `create'so thats undefined I know it needs to get defined somewhere. I've required 'devise' in routes but need a littlehelp setting this up. im assuming this worked at some point or was about to work and then got abandoned because devise is installed and all the code is there. here are my controllers and views for my users
users_controller.rb
class Admin::UsersController < InheritedResources::Base
before_filter :admin_only
actions :index, :show, :new, :edit, :create, :update, :destroy
respond_to :html
def create
@user = User.new(params[:user])
UserMailer.deliver_registration_confirmation(@user)
@user.save(false)
respond_to do |format|
format.html{ redirect_to admin_users_path}
end
end
private
def collection
paginate_options ||= {}
paginate_options[:page] ||= (params[:page] || 1)
paginate_options[:per_page] ||= (params[:per_page] || 20)
@search = User.search(params[:search])
@users ||= @search.all.paginate(paginate_options)
end
endmodels/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :http_authenticatable, :token_authenticatable, :confirmable, :lockable, :timeoutable and :activatable
devise :registerable, :database_authenticatable, :recoverable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :distributor_id, :admin, :sales_rep
belongs_to :distributor
enddb/schema.rb
ActiveRecord::Schema.define(:version => 20111209182614) do
etc..
create_table "users", :force => true do |t|
t.string "email"
t.string "encrypted_password", :limit => 128
t.string "password_salt", :limit => 128
t.string "confirmation_token", :limit => 128
t.string "remember_token", :limit => 128
t.boolean "email_confirmed", :default => false, :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "distributor_id"
t.boolean "admin", :default => false
t.string "first_name"
t.string "last_name"
t.boolean "reset_password_on_login", :default => false
t.boolean "sales_rep", :default => false, :null => false
t.string "auth_token"
t.integer "invitation_id"
t.integer "invitation_limit"
end
add_index "users", ["email"], :name => "index_users_on_email"
add_index "users", ["id", "confirmation_token"], :name => "index_users_on_id_and_confirmation_token"
add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
endpasswords/new.html.erb
<h2>Forgot your password?</h2>
<% semantic_form_for resource_name, resource, :url => password_path(resource_name) do |f| %>
<% f.inputs do%>
<%= f.input :email %>
<% end%>
<% f.buttons do%>
<%= f.commit_button "Send me reset password instructions" %>
<% end %>
<% end %>
<%= render :partial => "shared/devise_links" %>and this is the page where you enter your email to get an email sent for password recovery. I've checked a railscast tut but, its starting one from scratch and I would like to use devise for this. Is there a quick way to hook up this already created device pw recovery tool here:
module Devise
module Models
# Recoverable takes care of reseting the user password and send reset instructions
# Examples:
#
# # resets the user password and save the record, true if valid passwords are given, otherwise false
# User.find(1).reset_password!('password123', 'password123')
#
module Recoverable
def self.included(base)
base.class_eval do
extend ClassMethods
end
end
# Update password saving the record and clearing token. Returns true if
def reset_password!(new_password, new_password_confirmation)
self.password = new_password
self.password_confirmation = new_password_confirmation
clear_reset_password_token if valid?
save
end
# Resets reset password token and send reset password instructions by email
def send_reset_password_instructions
generate_reset_password_token!
::DeviseMailer.deliver_reset_password_instructions(self)
end
protected
# Generates a new random token for reset password
def generate_reset_password_token
self.reset_password_token = Devise.friendly_token
end
# Resets the reset password token with and save the record without validating
def generate_reset_password_token!
generate_reset_password_token && save(false)
end
# Removes reset_password token
def clear_reset_password_token
self.reset_password_token = nil
end
module ClassMethods
# Attempt to find a user by it's email.
def send_reset_password_instructions(attributes={})
recoverable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found)
recoverable.send_reset_password_instructions unless recoverable.new_record?
recoverable
end
# Attempt to find a user by it's reset_password_token
def reset_password_by_token(attributes={})
recoverable = find_or_initialize_with_error_by(:reset_password_token, attributes[:reset_password_token])
recoverable.reset_password!(attributes[:password], attributes[:password_confirmation]) unless recoverable.new_record?
recoverable
end
end
end
end
endhow would I fix this error and use the devise pw recovery code?