ruby-on-railssorcery

Defining mailer in sorcery


I am building a rails app using sorcery. When I go to users/new, there is an Argument Error stating "To use reset_password submodule, you must define a mailer (config.reset_password_mailer = YourMailerClass)"

Went into config/initializers/sorcery.rb and included code user.reset_password_mailer = UserMailer but I am still receiving the error. Within the UserMailer class, I defined reset_password_email method

 def reset_password_email(user)
    @user = User.find user.id
    @url  = edit_password_reset_url(@user.reset_password_token)
      mail(:to => user.email,
           :subject => "Your password has been reset")
end

and updated the sorcery.rb file

user.reset_password_email_method_name = :reset_password_email

I am still receiving the same error message.

In the Users controller:

class UsersController < ApplicationController

  def index
    @users = User.all
  end

  def new
    @user = User.new
  end


  def create
    @user = User.new(user_params)
        if @user.save
            redirect_to products_url, notice: "Signed Up!"
        else
            render "new"
        end
  end

  def show
    @user = User.find(params[:id])
  end

  private

  def user_params
    params.require(:user).permit(:email, :user_name, :password, :password_confirmation)
  end

end

And my User model is:

class User < ActiveRecord::Base
  authenticates_with_sorcery!
  has_many :projects

  validates :password, confirmation: true
  validates :password_confirmation, presence: true
  validates :email, uniqueness: true

end

Solution

  • It looks like you followed the Sorcery docs to configure the mailer. But keep in mind that config/initializers/sorcery.rb, like all of the files in config/initializers will only be processed when the application starts.

    It looks like your configuration is correct, and I get the same error if I run the application before I add the configuration, but it does work for me after restarting the Rails server.