dsomel21
dsomel21

Reputation: 626

ActionMailer not working after upgrade Rails & Ruby version. Only works in terminal

I just upgraded my app from Rails 4.0 to 4.2.6 and our Ruby version to 2.3.7.

As I am testing my app, I am noticing my reset password emails are NOT being sent out. Take a look.

password_resets_controller.rb

def create
  @user = User.find_by_email(params[:email])
  if @user
    @user.deliver_password_reset_instructions!
  end
  flash[:notice] = "Instructions to reset your password have been emailed to you"
  render :action => :new
end

This deliver_password_reset_instructions! exists in my User model file.

user.rb (hasn't been changed in over a year)

def deliver_password_reset_instructions!
  reset_perishable_token! #this method updates the user's perishable_token 
  NotificationMailer.send_reset_instructions(self)
end

But... when I call my send_reset_instructions method in the NotificationMailer model, nothing happens anymore. However... when I put a debugger in my code, as such:

def deliver_password_reset_instructions!
  reset_perishable_token! #this method updates the user's perishable_token 
  binding.pry # If I call `NotificationMailer.send_reset_instructions(self)` it works
  NotificationMailer.send_reset_instructions(self)
end

and I run the NotificationMailer.send_reset_instructions(self), it works, but not without me typing it.

It just seems realyl wierd to me that... it works... but only if it I manually type it in the debugger. So, I can't put my finger on WHERE it is broken.

Upvotes: 1

Views: 128

Answers (1)

Stefan Daschek
Stefan Daschek

Reputation: 376

ActionMailer changed in Rails 4.2, quoting the upgrade guide:

Previously, calling a mailer method on a mailer class will result in the corresponding instance method being executed directly. With the introduction of Active Job and #deliver_later, this is no longer true. In Rails 4.2, the invocation of the instance methods are deferred until either deliver_now or deliver_later is called.

So in your case, simply appending .deliver_now to your call should work:

NotificationMailer.send_reset_instructions(self).deliver_now

Upvotes: 2

Related Questions