使用 Rails 设计的密码重置电子邮件未更改
Password reset email using Rails devise is not changed
假设
我正在发送一封电子邮件以使用 devise token auth
重置我的密码。我能够发送电子邮件,但无法更改电子邮件模板。我都试过了还是不能解决问题,所以才来问这个问题。
我们要解决的问题
我想更改电子邮件模板以允许访问 link。
代码
路线
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>
电子邮件已发送
Hello test@example.com!
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.
我们尝试过的
我添加了一个设置并更改了使用的模板,但它并没有改变
/app/mailrs/devise_my_mailer.rb
class DeviseMyMailer < Devise::Mailer
default template_path: 'devise/mailers'
end
/config/initializers/devise.rb
・
・
・
config.mailer = 'DeviseMyMailer'
按照以下步骤覆盖 Devise 视图。我还建议检查他们的 documentation 关于覆盖的内容。
步骤 1
rails generate devise:views
步骤 2 在 config/initializers/devise.rb
下添加以下行
config.scoped_views = true
步骤 3
在 devise/sessions/new
下找到 Devise 视图并覆盖它们。
假设
我正在发送一封电子邮件以使用 devise token auth
重置我的密码。我能够发送电子邮件,但无法更改电子邮件模板。我都试过了还是不能解决问题,所以才来问这个问题。
我们要解决的问题
我想更改电子邮件模板以允许访问 link。
代码
路线
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>
电子邮件已发送
Hello test@example.com!
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.
我们尝试过的
我添加了一个设置并更改了使用的模板,但它并没有改变
/app/mailrs/devise_my_mailer.rb
class DeviseMyMailer < Devise::Mailer
default template_path: 'devise/mailers'
end
/config/initializers/devise.rb
・
・
・
config.mailer = 'DeviseMyMailer'
按照以下步骤覆盖 Devise 视图。我还建议检查他们的 documentation 关于覆盖的内容。
步骤 1
rails generate devise:views
步骤 2 在 config/initializers/devise.rb
config.scoped_views = true
步骤 3
在 devise/sessions/new
下找到 Devise 视图并覆盖它们。