【问题标题】:Devise breaks when resetting password重置密码时设计中断
【发布时间】:2025-12-04 20:25:01
【问题描述】:

我正在使用 devise,我认为一切正常,直到我试图重置密码。我收到以下错误。设计找不到名为布局/电子邮件的模板。为什么它甚至想要那个?我的印象是,在表单发布后,devise 会重定向到用户登录页面。

我怀疑设计无法找到“reset_password_instructions.html.haml”文件,因为它显然能够生成重置令牌,但实际上没有发送电子邮件。根据下面的错误消息,这听起来像是正确的诊断吗?

我该怎么做才能解决这个问题?我猜我必须通过覆盖控制器来修改设计,但我不确定。有什么建议吗?

Started POST "/users/password" for 127.0.0.1 at 2012-08-06 05:52:42 -0500
    Processing by Devise::PasswordsController#create as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"kGvuZiu+iu1HAx9xf4RsISj2SM410uLRoR6RbiJcBQw=", "user"=>{"email"=>"test1@example.com"}, "commit"=>"Send me reset password instructions"}
      User Load (6.0ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'test1@example.com' LIMIT 1
      User Load (1.3ms)  SELECT "users".* FROM "users" WHERE "users"."reset_password_token" = '5QZVFAsq2ev22gpQfe9i' LIMIT 1
       (0.1ms)  BEGIN
       (3.0ms)  UPDATE "users" SET "reset_password_token" = '5QZVFAsq2ev22gpQfe9i', "reset_password_sent_at" = '2012-08-06 10:52:42.591763', "updated_at" = '2012-08-06 10:52:42.593474' WHERE "users"."id" = 2
       (0.5ms)  COMMIT
    DEPRECATION WARNING: Passing a template handler in the template name is deprecated. You can simply remove the handler name or pass render :handlers => [:haml] instead. (called from call at /Users/bendowney/.rvm/gems/ruby-1.9.3-p194@global/gems/sass-3.1.19/lib/sass/plugin/rack.rb:54)
    DEPRECATION WARNING: Passing a template handler in the template name is deprecated. You can simply remove the handler name or pass render :handlers => [:haml] instead. (called from call at /Users/bendowney/.rvm/gems/ruby-1.9.3-p194@global/gems/sass-3.1.19/lib/sass/plugin/rack.rb:54)
    Completed 500 Internal Server Error in 113ms

    ActionView::MissingTemplate (Missing template layouts/email with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in:
      * "/Users/bendowney/Sites/RobotimusApp/app/views"
      * "/Users/bendowney/.rvm/gems/ruby-1.9.3-p194@RobotimusApp/gems/devise-2.1.2/app/views"
    ):
      actionpack (3.2.5) lib/action_view/path_set.rb:58:in `find'
      actionpack (3.2.5) lib/action_view/lookup_context.rb:109:in `find'
      actionpack (3.2.5) lib/action_view/renderer/abstract_renderer.rb:3:in `find_template'
      actionpack (3.2.5) lib/action_view/renderer/template_renderer.rb:79:in `resolve_layout'
      actionpack (3.2.5) lib/action_view/renderer/template_renderer.rb:86:in `resolve_layout'
      actionpack (3.2.5) lib/action_view/renderer/template_renderer.rb:69:in `block in find_layout'
      actionpack (3.2.5) lib/action_view/lookup_context.rb:228:in `with_layout_format'
#...etc

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    Devise 在重定向用户之前发送一封带有重置令牌的电子邮件。发送电子邮件还需要模板和布局。在您的情况下,此步骤失败是因为在您的代码中的某处,您可能为 ActionMailer 类指定了layout 'email',但实际上并未创建此布局模板。

    尝试添加至少包含以下内容的文件app/views/layouts/email.html.haml

    = yield
    

    (或 .html.erb 模板中的 <%= yield %>)。

    【讨论】:

    • 谢谢!这解决了我的问题。 (尽管出于某种原因,我实际上不能只拥有一个空文件。我需要稍微填写一下,就好像它是 layout/application.html 的电子邮件等价物一样。)
    • 当然,感谢您指出这一点 - 我相应地编辑了我的答案。