gpt4 book ai didi

ruby-on-rails - ruby /Rails : How do you customize the mailer templates of Devise?

转载 作者:行者123 更新时间:2023-12-03 06:09:06 27 4
gpt4 key购买 nike

我已经为我的 Rails 应用程序 (3.0.1) 安装了 Devise,它基本上可以正常工作。我只是似乎无法自定义邮件程序 View

  • 我的用户模型是“User”。
  • 设计 Controller (我需要覆盖它,以便我可以告诉 Controller 要使用什么布局文件)位于 app/controllers/users/ 中,就像这样 app/controllers/users/sessions_controller.rb
  • 设计 View (我编辑过的)位于 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.

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 "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

和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>

另外,如果有帮助的话,这里是我重写的 Controller :

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

如何解决这个问题?

谢谢!

最佳答案

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

rails generate devise:views

这将生成 Devise 使用的所有 View (包括邮件程序模板),您现在可以自定义这些 View 。

您要查找的邮件程序应该位于“app/views/devise/mailer”中

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

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

rails generate devise:views users

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

rails generate devise:views -v registrations confirmations

关于ruby-on-rails - ruby /Rails : How do you customize the mailer templates of Devise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4276233/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com