Devise Reconfirmable - alternative email is not picked - ruby-on-rails

I am allowing users to change their email in Devise.
Since this commit : https://github.com/heartcombo/devise/commit/ce071502eeb5c5c5199436f6694bfb6103010c4a it is possible to send an alternative email than the usual "Welcome" Registration email.
Devise mailer now includes a specific method for this: https://github.com/heartcombo/devise/blob/master/app/mailers/devise/mailer.rb
I have then created 2 different views in my my_model/views/mailer folder: email_changed.html.erb and email_changed.text.erb
Though this alternative email is never picked. The original Welcome email is sent instead...
Am I missing something ?
===
My Devise version is 4.7.3 (also I am coming from an old version)
and my devise initializer has config.reconfirmable = true
I have seen this hack but I guess it shouldn't need be used anymore : How to send two different emails for devise confirmable and devise reconfirmable?

Is that my_model/views/mailer folder right? according to the code (*) it should be views/my_model/mailer.
https://github.com/heartcombo/devise/blob/45b831c4ea5a35914037bd27fe88b76d7b3683a4/lib/devise/mailers/helpers.rb#L66
I would suggest you to copy that helper file inside your project so you can put some byebug statement and properly debug it.

Related

Customize Devise Passwords error message

I've a project that implements devise and I'm having trouble overriding the Passwords controller's messages.
When a wrong email address is entered by the user, Devise by default gives
Unable to find user with email {email}
I cant find the key to override this message in the devise.en.yml and can't find anything with the similar message on the devise repo as well.
Do I have an option other than to override the controller? Any help is appreciated.
Thanks
Add a new locale having the following structure:
en:
devise_token_auth:
passwords:
user_not_found: "Your custom message"
This message will be displayed when an invalid email is passed in the forgot password form.
Originally this message comes from the devise_token_auth gem. But if you have the same locale in your locale files, it will override the gem's locale.
It doesn't matter which *.en.yml file you will put this locale into. It can be devise.en.yml, or you can add a new file called devise_token_auth.en.yml. Only the structure matters.
as described in the following link you can generate the devise controller with
rails generate devise:controllers user
then you can over-ride the create or update action. If you include super in the new action, it will call the parent controller action and then execute the code.
You can also over-ride the devise messages as explained in this post, you just need to create that locale in your settings
en:
devise_token_auth:
sessions:
not_confirmed: "your message overwritten"

Rails ActionMailer : filter emails to a specific list in development

Without editing any of my /app files, I'd like to edit either development.rb or an initializer where I set a whitelist of testers.
Then the emails are sent only to those people in the whitelist (not to spam other users mailbox).
I though of overriding deliver!, or the user.get_mail method but :
it's not in /config where it should be
it doesn't filter gem generated emailing (ie devise, mailboxer etc.)
You might want to check out Action mailer interceptors
And do something like this :
class BetaEmailInterceptor
def self.delivering_email(message)
message.perform_deliveries = false unless WHITELIST.include?(message.to.first)
end
end
And
ActionMailer::Base.register_interceptor(BetaEmailInterceptor)
This is a very naive implementation and will only work if the first recipient is whitelisted but you get the idea.

Ruby on Rails Devise Gem, specify email template from a controller

I have a controller which inherited from ApplicationController
where I send reset password insturctions, is there any way to specify template?
#user=User.find_by_email(email);
if #user.nil?
render :status=>404, :json=>{:message=>"User not found or email format is invalid."}
return
else
#user.devise_mailer.reset_password_instructions(#user).deliver
render :status=>200, :json=>{:message=>"Reset password instructions have been sent."}
end
As you see in the snippet above, #user.devise_mailer.reset_password_instructions(#user).deliver
Sends reset password instructions, but it takes default email template, which I don't wanna change. I need to create another template and somehow specify to use it
Create your own mailer then. See http://guides.rubyonrails.org/action_mailer_basics.html for details. Once you have a new mailer, in the controller, instead of:
#user.devise_mailer.reset_password_instructions(#user).deliver
you'd do something like:
YourMailer.reset_password_instructions(#user).deliver
You may want to reuse parts of Devise in your mailer: check out devise sources for that: https://github.com/plataformatec/devise/blob/master/lib/devise/mailers/helpers.rb
Another thing you may try is redefining devise template by putting a file named app/views/devise/mailer/reset_password_instructions.html.erb in your application directory. Of course you can base your implementation on devise sources as well: https://github.com/plataformatec/devise/blob/master/app/views/devise/mailer/reset_password_instructions.html.erb

Rails + Devise => Use Devise Mailer globally to handle all of the mail sending?

I would like to use the devise mailer globally,
since that way I would not have to add another mailer to my app and just use the devise one, to send custom mails outside the devise views/controllers.
Already digged to long for this, anyone know how I can make this possible?
If the point is to only have one mailer, it's perhaps more straightforward to have your one single mailer just extend the DeviseMailer. E.g. in app/mailer
# app/mailers/mailer.rb
class Mailer < Devise::Mailer
# add all your custom mailer methods
end
This setup will, for example, cause all emails (from Devise and otherwise) to all be sent with the app/layouts/mailer.html.erb template.
Note that to do this you do need to move the Devise mailer templates from app/views/devise/mailer to just app/views/mailer.

How to use two different sets of mailer templates for two different Devise models?

I'm using Resque to send Devise emails. To do so I set:
devise.rb
config.mailer = "Mailers::Devise::DeviseMailer"
But I have 2 devise models -- User and Client. I want to send different devise mailer templates for each.
devise.rb says "The first four configuration values can also be set straight in your models.", and config.mailer is one of those first four, but I'm not sure how to set that in the model directly.
How can I do this?
I have created an example app that does this:
https://github.com/rodrigoflores/multiple-mailer
Please pay attention to https://github.com/rodrigoflores/multiple-mailer/blob/master/app/models/user.rb#L6 and the similar line on app/models/admin.rb where you can choose the mailer for each devise model. You will also have to read Devise mailers (https://github.com/plataformatec/devise/blob/master/app/mailers/devise/mailer.rb) source code to implement the same methods. And finally, write your views for the mailers.
The trick was to set:
devise.rb
config.scoped_views = true
For more info see: 'Configuring views' in the devise doc at https://github.com/plataformatec/devise
If you have more than one role in your application (such as “User” and
“Admin”), you will notice that Devise uses the same views for all
roles. Fortunately, Devise offers an easy way to customize views. All
you need to do is set “config.scoped_views = true” inside
“config/initializers/devise.rb”.
After doing so, you will be able to have views based on the role like
“users/sessions/new” and “admins/sessions/new”. If no view is found
within the scope, Devise will use the default view at
“devise/sessions/new”. You can also use the generator to generate
scoped views:

Resources