Ruby/Rails:你如何定制 Devise 的邮件模板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4276233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby/Rails: How do you customize the mailer templates of Devise?
提问by Ramon Tayag
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.
我已经为我的 Rails 应用程序 (3.0.1) 安装了 Devise,它大部分都在工作。我似乎无法自定义邮件程序视图。
- 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:
- 我的用户模型是“用户”。
- 设计控制器(我需要覆盖它以便我可以告诉控制器要使用的布局文件)在 中
app/controllers/users/,就像这样app/controllers/users/sessions_controller.rb - 设计视图(我已经编辑过)
app/views/users/就像这样app/views/users/registrations/new.html.haml - 这是我的路由文件的设计部分:
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.
至少上面的一切都有效。但是,在发送邮件时,Devise 似乎使用的模板不是我在app/views/users/mailer/. Devise 似乎仍然选择默认的(好像我从未编辑过文件)。我猜 Devise 仍然使用 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:
和 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!
谢谢!
回答by David Sulc
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.
这将生成 Devise 使用的所有视图(包括邮件程序模板),您现在可以自定义这些视图。
The mailers you're looking for should then be in 'app/views/devise/mailer'
您正在寻找的邮件程序应该在“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:
如果您想生成范围视图,或者仅生成其中的一个子集也是可能的。根据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.
如果您只想生成几组视图,例如可注册和可确认模块的视图,您可以使用 -v 标志将模块列表传递给生成器。
rails generate devise:views -v registrations confirmations
回答by akbarbin
To generate views by resource name
按资源名称生成视图
rails generate devise:views users
To generate specify views by moduleof recoverable
为了产生由指定的观点模块的recoverable
rails generate devise:views users -v passwords
To generate specify mailviews only
仅生成指定邮件视图
rails generate devise:views users -v mailer
for more details generate views
有关更多详细信息,请生成视图
回答by Brian
according to devise's docs
根据设计的文档
you should edit your config/initializers/devise.rb:
你应该编辑你的 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.
通过这样做,您可以为不同的模型自定义视图,而不是全局设计。
回答by Andrew Gertig
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 Postmarkyou will need to create a custom Mailerthat subclasses Devise::Mailerand overrides its "notification" methods.
如果你有兴趣在不使用设计的的ActionMailer的默认的使用而会像送定制的电子邮件使用API从像服务SendGrid,Mailgun,或邮戳,你需要创建一个自定义的Mailer子类Devise::Mailer,并覆盖其“通知”的方法。
Here is an example using Mailgun.
这是一个使用 Mailgun的例子。
回答by Cem Baykam
Try this:
尝试这个:
rails generate devise:views

