Ruby/Rails: How do you customize the mailer templates of Devise?
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 soapp/controllers/users/sessions_controller.rb
- The devise views (which I've edited) are in
app/views/users/
like soapp/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!
Solution 1:
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
Solution 2:
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
Solution 3:
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.