Search code examples
ruby-on-rails-4actionmailerrailstutorial.org

ArgumentError in Rails::MailersController#preview (wrong number of arguments (1 for 0))


I am working on Chapter 10 of the Rails Tutorial. I've skipped the account activation and implemented the password reset mailer. Here is the code: of user_mailer_preview.rb

# Preview all emails at http://localhost:3000/rails/mailers/user_mailer
class UserMailerPreview < ActionMailer::Preview

  # Preview this email at
  # http://localhost:3000/rails/mailers/user_mailer/password_reset
  def password_reset
    user = User.first
    user.reset_token = User.new_token
    UserMailer.password_reset(user)
  end
end

Here is the code of the user mailer itself:

class UserMailer < ActionMailer::Base
  default from: "from@example.com"

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.user_mailer.password_reset.subject
  #
  def password_reset
    @greeting = "Hi"

    mail to: "to@example.org"
  end
end

When I go to http://localhost:3000/rails/mailers/user_mailer/password_reset I am getting

ArgumentError in Mailer

What am I doing wrong?


Solution

  • You are calling UserMailer.password_reset(user) and passing in the user

    but your password_reset method does not take an argument. So either you will need to change your password_reset to take a user (and do something with it)

    def password_reset(user)
       #do something with the user, such as send to their email address  
       @greeting = "Hi"
       mail to: user.email 
    end
    

    or adjust your call to be UserMailer.password_reset without the user.