Custom variables in Devise reset password instructions?

Viewed 3964

I need to be able to customise the rails devise mailer view for reset password instructions.

for this I need to do two things.

  1. Specify a custom URL for the link, so that its a host/domain based on a certain business logic. This host and domain comes from the URL in the browser, i.e. the request object, when the user clicks forgot password. So I do not have the request object in delayed_job to process it as I need, hence I need to be able to do this at some point in the delayed_job that is sending the email.

  2. Pass custom variables to the mailer view, so that I can add in various other logic for the view, hiding and showing bits as I need.

Can anyone help? I can see that you can generate the mailer views for devise, but I need to be able pass over various items to it also. Do I need to somehow override the functions myself in my User model and password controller for example?

5 Answers

Overriding the whole controller method and adding param in send_reset_password_instructions opts parameters will fix it.

@resource.send_reset_password_instructions(
    email: @email,
    provider: 'email',
    redirect_url: @redirect_url,
    client_config: params[:config_name],
    parameter_passed: params[:parameter_passed],
  )

You can access the param in the view as message['parameter_passed']

You just need to add a flag to display in the view mailer. From here you can just call a method and pass the parameter.

@user.send_reset_password_instructions("true")

Now override the method send_reset_password_instructions

def send_reset_password_instructions(option = nil)
  token = set_reset_password_token
  send_reset_password_instructions_notification(token, option)
  token 
end

def send_reset_password_instructions_notification(token, option = nil)
  send_devise_notification(:reset_password_instructions, token, :option => option)
end

Then you can access the parameter by using:

message[:option]
Related