本文介绍了Ruby/Rails:你如何定制 Devise 的邮件模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的 Rails 应用程序 (3.0.1) 安装了 Devise,它大部分都在工作.我只是似乎无法自定义邮件程序视图.

  • 我的用户模型是用户".
  • 设计控制器(我需要覆盖它以便我可以告诉控制器要使用的布局文件)位于 app/controllers/users/ 中,就像 app/controllers/users/sessions_controller.rb
  • 设计视图(我已经编辑过)在 app/views/users/ 中,就像这样 app/views/users/registrations/new.html.haml
  • 这是我的路由文件的设计部分:
devise_for :users, :controllers => {:sessions => "用户/会话",:registrations => "用户/注册",:passwords => "用户/密码",:confirmations => "用户/确认",:unlocks => "用户/解锁"} 做获取/登录"=>设计/会话#new"得到/注销"=>设计/会话#destroy"结尾

至少上面的一切都有效.但是,在发送邮件时,Devise 似乎使用的模板不是我在 app/views/users/mailer/ 中编辑的模板.Devise 似乎仍然选择默认的(好像我从未编辑过文件).我猜 Devise 仍然使用 gem 中的文件.

如果有帮助,这里是黄瓜错误:

功能:管理帐户为了管理帐户用户应该可以注册# 默认情况下,www.example.com 是测试时的主机.# 这是一个问题,因为当我们的站点搜索域example.com 时,它找不到任何.# 因此,我们必须将我们的测试域设置为我们选择的一个(并在路由中提及),或者创建一个域 example.com# 我更喜欢第一个选项.场景:注册并重置密码# features/manage_accounts.feature:10鉴于我在登录页面 # features/step_definitions/web_steps.rb:19当我按照注册"# features/step_definitions/web_steps.rb:33我用bobrobcom"填写登录"# features/step_definitions/web_steps.rb:39我用[email protected]"填写电子邮件"# features/step_definitions/web_steps.rb:39我用123456"填写密码"# features/step_definitions/web_steps.rb:39我用123456"填写密码确认"# features/step_definitions/web_steps.rb:39然后我按注册"# features/step_definitions/web_steps.rb:27然后我应该看到您的帐户已创建.确认已发送到您的电子邮件."# features/step_definitions/web_steps.rb:107我应该会收到一封电子邮件 # features/step_definitions/email_steps.rb:51当我打开电子邮件时 # features/step_definitions/email_steps.rb:72然后我应该会看到欢迎 bobrobcom!"在电子邮件正文中 # features/step_definitions/email_steps.rb:96预期<p>欢迎使用 [email protected]!</p>

<p>您可以通过以下链接确认您的帐户:</p>

<p><ahref="http://stils.dev/users/confirmation?confirmation_token=d9ZXliqfTArb2cNmwPzL">确认我的帐户</a></p>
" 以包含Welcome bobrobcom!"(RSpec::Expectations::ExpectationNotMetError)./features/step_definitions/email_steps.rb:97:in `/^(?:I|they) 应该在电子邮件正文中看到([^"]*?)"$/'features/manage_accounts.feature:21:in `然后我应该看到欢迎 bobrobcom!"在电子邮件正文中"当我按照确认我的帐户"# features/step_definitions/web_steps.rb:33然后我应该看到您的帐户已成功确认.您现在已登录."# features/step_definitions/web_steps.rb:107当我退出时 # features/step_definitions/user_steps.rb:9然后我转到重置密码页面 # features/step_definitions/web_steps.rb:23我用[email protected]"填写电子邮件"# features/step_definitions/web_steps.rb:39然后我按发送给我重置密码说明"# features/step_definitions/web_steps.rb:27然后我应该会看到您将在几分钟内收到一封电子邮件,其中包含有关如何重置密码的说明".# features/step_definitions/web_steps.rb:107我应该会收到一封电子邮件 # features/step_definitions/email_steps.rb:51当我打开电子邮件时 # features/step_definitions/email_steps.rb:72然后我应该看到你好 bobrobcom!"在电子邮件正文中 # features/step_definitions/email_steps.rb:96当我按照电子邮件中的更改我的密码"# features/step_definitions/email_steps.rb:166然后我应该看到设置您的新密码"# features/step_definitions/web_steps.rb:107失败场景:黄瓜 features/manage_accounts.feature:10 # 场景:注册并重置密码

还有 app/views/users/confirmation_instructions.erb:

欢迎!

<p>您可以通过以下链接确认您的帐户:</p><p><%= link_to 'Confirm my account',confirmation_url(@resource, :confirmation_token =>@resource.confirmation_token)%></p>

此外,如果有帮助,以下是我覆盖的控制器:

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

我该如何解决这个问题?

谢谢!

解决方案

我认为您需要自己管理设计视图.在控制台中尝试以下操作:

rails 生成设计:视图

这将生成 Devise 使用的所有视图(包括邮件模板),您现在可以对其进行自定义.

您要查找的邮件程序应该在app/views/devise/mailer"中

如果您想生成范围视图,或者仅生成其中的一个子集也是可能的.根据 https://github.com/plataformatec/devise#configuring-views 上的文档:

您还可以使用生成器生成范围视图:

rails 生成 devise:views 用户

如果您只想生成几组视图,例如可注册和可确认模块的视图,您可以使用 -v 标志将模块列表传递给生成器.

rails 生成 devise:views -v 注册确认

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>

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

<p><a href="http://stils.dev/users/confirmation?confirmation_token=d9ZXliqfTArb2cNmwPzL">Confirm my account</a></p>
" 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!

解决方案

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

这篇关于Ruby/Rails:你如何定制 Devise 的邮件模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:04