Home > Software design >  Password reset email using Rails devise is not changed
Password reset email using Rails devise is not changed

Time:05-03

Assumption

I am sending an email to reset my password using devise token auth. I was able to send the email, but I am unable to change the email template. I have tried everything but could not solve the problem, so I am asking this question.

What we want to solve

I want to change the email template to allow access to the link.

Code

routes


  namespace :api do
    namespace :v1 do
      ・
            ・
            ・
      namespace :auth do
        resource :passwords, only: [:create, :update]
      end

/models/user.rb

# frozen_string_literal: true
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  has_many :posts, dependent: :destroy
  has_many :schedules, dependent: :destroy
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  include DeviseTokenAuth::Concerns::User

controllers/api/v1/auth/passwords_controller.rb


class Api::V1::Auth::PasswordsController < ApplicationController
  skip_before_action :authenticate_api_v1_user_from_token!, only: [:create], raise: false
  def create
    user = User.find_by(email: create_params[:email])
    user&.send_reset_password_instructions
    render json: {}
  end

  def update
    user = User.reset_password_by_token(update_params)
    render json: user, status: :ok
  end

  private
    def create_params
      params.require(:user).permit(:email)
    end

    def update_params
      params.require(:user).permit(:password, :password_confirmation)
    end
end


/views/devise/mailers/reset_password_instructions.html.erb


<p>Hello <%= @resource.email %>!</p>

<p>
test
</p>

<p>
  <%= link_to 'Change my password', "http://localhost:8080/user/edit/newPassword?token=#{@token}"%>
</p>

<p>If you didn't request this, please ignore this email.</p>
<p>
  Your password won't change until you access the link above and create a new
  one.
</p>

Email sent

Hello [email protected]!

A password change request has been sent. You can change your password from the link below.

change the password

If you don't remember this content, please ignore this email.

Your current password will not change until you access the link above to create a new password.

What we have tried

I added a setting and changed the template used, but it did not change

/app/mailrs/devise_my_mailer.rb

class DeviseMyMailer < Devise::Mailer
  default template_path: 'devise/mailers'
end

/config/initializers/devise.rb


・
・
・ 
config.mailer = 'DeviseMyMailer'  

CodePudding user response:

Follow the steps below to override Devise views. I would also recommend checking their documentation about overriding too.

Step 1

rails generate devise:views

Step 2 Add the following line under config/initializers/devise.rb

config.scoped_views = true

Step 3

Find the Devise views under devise/sessions/new and override them.

  • Related