Ruby/Rails:如何自定义Devise的邮件模板?

Ram*_*yag 48 ruby-on-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 => "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

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

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

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 "my@email.com"                                                                          # 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 my@email.com!</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 "my@email.com"                                                                          # 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
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

此外,如果它有帮助,这里是我已经覆盖的控制器:

| | |~users/
| | | |-confirmations_controller.rb
| | | |-passwords_controller.rb
| | | |-registrations_controller.rb
| | | |-sessions_controller.rb
| | | `-unlocks_controller.rb
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

谢谢!

Dav*_*ulc 62

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

rails generate devise:views
Run Code Online (Sandbox Code Playgroud)

这将生成Devise使用的所有视图(包括邮件程序模板),您现在可以自定义它们.

您正在寻找的邮件应该在'app/views/devise/mailer'中

如果您想生成范围视图,或者只想生成它们的子集.根据https://github.com/plataformatec/devise#configuring-views上的文档:

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

rails generate devise:views users
Run Code Online (Sandbox Code Playgroud)

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

rails generate devise:views -v registrations confirmations
Run Code Online (Sandbox Code Playgroud)

  • 嗨,我和海报有同样的问题,这个答案不是真正的答案.我有一个解决方案的黑客,这是将我的观点/用户符号链接到views/devise; 有了它,设计使用自定义的邮件模板.但是这个解决方案并不干净,我不想同时拥有一个devise和users目录.其他人可以澄清吗? (2认同)

akb*_*bin 30

资源名称生成视图

rails generate devise:views users
Run Code Online (Sandbox Code Playgroud)

为了产生由指定的观点模块recoverable

rails generate devise:views users -v passwords
Run Code Online (Sandbox Code Playgroud)

仅生成指定邮件视图

rails generate devise:views users -v mailer 
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请生成视图


Bri*_*ian 22

根据设计的文档

你应该编辑你的config/initializers/devise.rb:

config.scoped_views = true
Run Code Online (Sandbox Code Playgroud)

(默认评论)

通过这样做,您可以自定义不同模型的视图,而不是全局设计.

  • 设置此配置选项后,运行`rails g devise:views [SCOPE]`其中SCOPE是单一化资源名称(例如用户,客户,管理员等). (2认同)