kai
kai

Reputation: 21

Ruby on Rails with Sorcery reset password email has a undefined method error

Using Sorcery 0.7.4 with Rails 3.1.1 for authentication. Everything was going well until I tried to setup password resetting.

Activation works perfectly and emails are sent, but for some reason I get this error when trying to send the reset password email.

undefined method `reset_password_email' for nil:NilClass

I copied the tutorial exactly, and when I did a quick test in the console it shot off the email as expected. In console:

user = User.find(1)
user.deliver_reset_password_instructions!

In the actual controller, it finds the user by the email submitted from the form and in the log I can see it is retrieving the right user and setting the token, but errors out as above and rolls back.

I checked the gem's code for deliver_reset_password_instructions! and there seems to be no reason for it to fail.

PasswordResetsController:

@user = User.find_by_email(params[:email])
@user.deliver_reset_password_instructions! if @user

The following is copied from the gem code:

Instance Method in Gem:

def deliver_reset_password_instructions!
    config = sorcery_config
    # hammering protection
    return false if config.reset_password_time_between_emails && self.send(config.reset_password_email_sent_at_attribute_name) && self.send(config.reset_password_email_sent_at_attribute_name) > config.reset_password_time_between_emails.ago.utc
    self.send(:"#{config.reset_password_token_attribute_name}=", TemporaryToken.generate_random_token)
    self.send(:"#{config.reset_password_token_expires_at_attribute_name}=", Time.now.in_time_zone + config.reset_password_expiration_period) if config.reset_password_expiration_period
    self.send(:"#{config.reset_password_email_sent_at_attribute_name}=", Time.now.in_time_zone)
    self.class.transaction do
        self.save!(:validate => false)
        generic_send_email(:reset_password_email_method_name, :reset_password_mailer)
    end
end


The method called above for mailing:

def generic_send_email(method, mailer)
    config = sorcery_config
    mail = config.send(mailer).send(config.send(method),self)
    if defined?(ActionMailer) and config.send(mailer).superclass == ActionMailer::Base
        mail.deliver
    end
end

Again all the required mailer bits and pieces are there and work from the console.

Upvotes: 2

Views: 3090

Answers (2)

Stefano Bernardi
Stefano Bernardi

Reputation: 61

Uncomment this lines in the sorcery initializer

user.reset_password_mailer = UserMailer  
user.reset_password_email_method_name = :reset_password_email

Upvotes: 3

gozman
gozman

Reputation: 1

Check your app/mailers/user_mailer.rb file.

If you were following the tutorial you probably did something like copy and paste the method definition from the wiki (which takes one parameter) into the generated method definition (which doesn't take any parameter), hence the 1 for 0 ArgumentError.

In other words, you likely have something that looks like this:

def reset_password_email
   def reset_password_email(user)

This is bad, but an easy fix :-)

Upvotes: 0

Related Questions