Ruby/Rails: How do you customize the mailer templates of Devise?
Asked Answered
F

6

50

I've installed Devise for my Rails app (3.0.1) and it's mostly working. I just can't seem customize the mailer views.

  • My user model is "User".
  • The devise controllers (which I needed to override so I could tell the controllers what layout file to use) are in app/controllers/users/, like so app/controllers/users/sessions_controller.rb
  • The devise views (which I've edited) are in app/views/users/ like so app/views/users/registrations/new.html.haml
  • Here's the devise portion of my routes file:
    devise_for :users, :controllers => { 
      :sessions => "users/sessions", 
      :registrations => "users/registrations", 
      :passwords => "users/passwords", 
      :confirmations => "users/confirmations", 
      :unlocks => "users/unlocks"
    } do
      get "/login" => "devise/sessions#new"
      get "/logout" => "devise/sessions#destroy"
    end

Everything above works, at least. However, when sending mail, the templates that Devise seems to use aren't the ones I've edited at app/views/users/mailer/. Devise still seems to pickup the default one (as if I've never edited the files). I'm guessing that Devise still uses the files in the gem.

In case it helps, here's the Cucumber error:

Feature: Manage accounts
  In order to manage accounts
  users
  should be able to signup

  # By default, www.example.com is the host when testing.
  # This is a problem because when our site searches for the domain example.com, it cant find any.
  # Therefore we must either set our testing domain to one of our choosing (and mention that in the routes), or create a domain example.com
  # I prefer the first option.
  Scenario: Signing up and resetting the password                                                                      # features/manage_accounts.feature:10
    Given I am on the login page                                                                                       # features/step_definitions/web_steps.rb:19
    When I follow "Sign up"                                                                                            # features/step_definitions/web_steps.rb:33
    And I fill in "Login" with "bobrobcom"                                                                             # features/step_definitions/web_steps.rb:39
    And I fill in "Email" with "[email protected]"                                                                          # features/step_definitions/web_steps.rb:39
    And I fill in "Password" with "123456"                                                                             # features/step_definitions/web_steps.rb:39
    And I fill in "Password confirmation" with "123456"                                                                # features/step_definitions/web_steps.rb:39
    And I press "Sign up"                                                                                              # features/step_definitions/web_steps.rb:27
    Then I should see "Your account has been created. A confirmation was sent to your e-mail."               # features/step_definitions/web_steps.rb:107
    And I should receive an email                                                                                      # features/step_definitions/email_steps.rb:51
    When I open the email                                                                                              # features/step_definitions/email_steps.rb:72
    Then I should see "Welcome bobrobcom!" in the email body                                                           # features/step_definitions/email_steps.rb:96
      expected "<p>Welcome [email protected]!</p>\n\n<p>You can confirm your account through the link below:</p>\n\n<p><a href=\"http://stils.dev/users/confirmation?confirmation_token=d9ZXliqfTArb2cNmwPzL\">Confirm my account</a></p>\n" to include "Welcome bobrobcom!" (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/email_steps.rb:97:in `/^(?:I|they) should see "([^"]*?)" in the email body$/'
      features/manage_accounts.feature:21:in `Then I should see "Welcome bobrobcom!" in the email body'
    When I follow "Confirm my account"                                                                                 # features/step_definitions/web_steps.rb:33
    Then I should see "Your account was successfully confirmed. You are now signed in."                                # features/step_definitions/web_steps.rb:107
    When I log out                                                                                                     # features/step_definitions/user_steps.rb:9
    And I go to the reset password page                                                                                # features/step_definitions/web_steps.rb:23
    And I fill in "Email" with "[email protected]"                                                                          # features/step_definitions/web_steps.rb:39
    And I press "Send me reset password instructions"                                                                  # features/step_definitions/web_steps.rb:27
    Then I should see "You will receive an email with instructions about how to reset your password in a few minutes." # features/step_definitions/web_steps.rb:107
    And I should receive an email                                                                                      # features/step_definitions/email_steps.rb:51
    When I open the email                                                                                              # features/step_definitions/email_steps.rb:72
    Then I should see "Hello bobrobcom!" in the email body                                                             # features/step_definitions/email_steps.rb:96
    When I follow "Change my password" in the email                                                                    # features/step_definitions/email_steps.rb:166
    Then I should see "Set your new password"                                                                          # features/step_definitions/web_steps.rb:107

Failing Scenarios:
cucumber features/manage_accounts.feature:10 # Scenario: Signing up and resetting the password

And app/views/users/confirmation_instructions.erb:

<p>Welcome <%= @resource.login %>!</p>

<p>You can confirm your account through the link below:</p>

<p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %></p>

Also, if it helps, here are the controllers I've overridden:

| | |~users/
| | | |-confirmations_controller.rb
| | | |-passwords_controller.rb
| | | |-registrations_controller.rb
| | | |-sessions_controller.rb
| | | `-unlocks_controller.rb

How do I fix this issue?

Thanks!

Finnie answered 25/11, 2010 at 10:56 Comment(0)
A
69

I think you'll need to manage the Devise views yourself. Try the following in a console:

rails generate devise:views

This will generate all the views Devise uses (including mailer templates), which you can now customize.

The mailers you're looking for should then be in 'app/views/devise/mailer'

If you want to generate scoped views, or only a subset of them that is also possible. Per the documentation at https://github.com/plataformatec/devise#configuring-views:

You can also use the generator to generate scoped views:

rails generate devise:views users

If you would like to generate only a few sets of views, like the ones for the registerable and confirmable module, you can pass a list of modules to the generator with the -v flag.

rails generate devise:views -v registrations confirmations
Admit answered 25/11, 2010 at 23:7 Comment(8)
I'm confused - since all my stuff are under the user namespace, should the views be under 'app/views/users/'? That's what I have now, and when I edit the views for the other stuff (app/views/users/registrations, for example) it reflects the change on my app. Not the app/views/users/mailer views though.Finnie
It seems you have the mailer templates in the right place. Look at this thread groups.google.com/group/plataformatec-devise/browse_thread/…, maybe it can point you in the right direction?Admit
hi, I'm having the same problem as the poster, and this answer is not really an answer. I have a hack of a solution, which is to symlink my views/users to views/devise; with that, devise uses the customized mailer templates. But that solution is not clean, and I don't want to have both a devise and users directory. Can anyone else clarify?Gan
When i call this rails generate devise:views , i do screw up a lot of work done in the app wich now is working lovely, is really no way to generate just the mails?Immure
Try creating a new empty project with devise (same Gemfile), generate the emails, then copy them over.Admit
According to devise's docs, you can generate a subset of views by running rails generate devise:views -v [controller1[ controller2]...], so, if you only need to override the mailer views, you should run rails generate devise:views -v confirmationsAspirate
This answer is incomplete. The given command is exporting ALL the device views. Please re-again the question's title and accept the answer from @akbarbin.Pernicious
The original answer (posted nearly 8 years ago) does indeed answer the question which is "customizing mailer templates in Devise". I have however updated my answer to also cover customizing only a subset of views.Admit
A
33

To generate views by resource name

rails generate devise:views users

To generate specify views by module of recoverable

rails generate devise:views users -v passwords

To generate specify mail views only

rails generate devise:views users -v mailer 

for more details generate views

Alexei answered 10/3, 2016 at 7:47 Comment(0)
F
28

according to devise's docs

you should edit your config/initializers/devise.rb:

config.scoped_views = true

(it is commented by default)

by doing so, you can customize your views for different models, rather than the global devise.

Fid answered 25/10, 2012 at 7:43 Comment(2)
After setting this config option, run rails g devise:views [SCOPE] where SCOPE is the singularised resource name (eg user, customer, admin etc).Rositaroskes
@Rositaroskes Please note that in the Devise documentation a pluralized scope is passed to the generator.Allenaallenby
S
1

If you are interested in not using Devise's default usage of ActionMailer and would instead like to send customized emails with an API from a service like SendGrid, Mailgun, or Postmark you will need to create a custom Mailer that subclasses Devise::Mailer and overrides its "notification" methods.

Here is an example using Mailgun.

Salinasalinas answered 29/1, 2019 at 2:13 Comment(0)
R
1

If you want to generate JUST the email views, you can do it with: rails g devise:views [SCOPE] -v mailer. The [SCOPE] in your case is users.

Note: I'm using Devise 4.7.3.

Reyreyes answered 13/10, 2020 at 17:2 Comment(0)
W
-1

Try this:

rails generate devise:views
Wrap answered 3/12, 2010 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.