Reputation: 121
I need to be able to customise the rails devise mailer view for reset password instructions.
for this I need to do two things.
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.
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?
Upvotes: 8
Views: 4953
Reputation: 1
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]
Upvotes: 0
Reputation: 201
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']
Upvotes: 4
Reputation: 386
I struggled with this too before I realized that declaring custom variables BEFORE calling super will work.
def reset_password_instructions(record, token, opts={})
@custom_variable = "Greetings, world"
# your gorgeous code
mailer_object = super
mailer_object
end
Upvotes: 2
Reputation: 313
I needed to add a source
to be included into the reset password view, here's what I implemented:
class User < ActiveRecord::Base
prepend ResetPasswordWithSource
devise :recoverable
....
end
module User::ResetPasswordWithSource
def send_reset_password_instructions(source=nil)
@source = source
super()
end
def send_devise_notification(notification, *args)
args.last.merge!({ source: @source })
super
end
end
From here you can just call user.send_reset_password_instructions('special_source')
And can access in views via @options[:source] = 'special_source'
Upvotes: 1
Reputation: 121
so, after much ado and searching and hacking around with stuff... this is just not possible. so I ended up writing my own mailer and bypassing the devise reset password methods in the controllers, to generate my own reset token, set my variables I needed, called my usermailer.... and embedded the devise url in my mail to get it back calling devise once the password reset link was clicked, and all was fine then....
I hated having to rewrite the logic, but in the end its the quickest and cleanest solution.
One approach that nearly worked, was using a non activerecord attribute on my user model to store the bits I needed and "hacking" that into the @resource in the devise view, but it was causing some grief in devise doing so, as a result, I went with the option above...
Upvotes: 1