Haml + ActionMailer - Rails?

3

我正在一个不使用Rails的项目中尝试使用ActionMailer,并且想在HTML邮件模板中使用Haml。有人成功配置和初始化过吗,以便可以找到并呈现模板?目前我遇到了如下错误:

ActionView::MissingTemplate: Missing template new_reg/daily_stats/full with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en]} in view paths "/home/petersen/new_reg/lib/new_reg/mailers/views"

为了澄清,这是ActionMailer 3.0.4。

你正在使用哪个版本的Rails? - Peer Allan
你能分享一下你的ActionMailer代码吗?在ActionMailer API文档中,它说:“像Action Controller一样,每个邮件类都有一个相应的视图目录,在该目录中,类的每个方法都会查找与其名称相同的模板。要定义用于邮件的模板,请在您的mailer模型中创建一个与该方法名称相同的.erb文件。” - Jochem Schulenklopper
@jschulenklopper:我已经成功配置了ActionMailer,让它查找一个有用的视图目录,但它并没有启用Haml或查找.haml扩展名。我只有一行require "haml" - dunedain289
4个回答

6

看起来问题是由于没有完整的Rails堆栈,Haml没有完全加载,特别是Haml :: Plugin类。在正常的 require 'haml'行之后添加 require 'haml/template/plugin'似乎可以解决问题。


1
对于未初始化常量Haml :: Template的类似问题的未来谷歌搜索者,请确保同时包含require'haml / template / options' - nicohvi

1
在Sinatra中,将require 'haml/template/plugin'放在"configure do"块中与ActionMailer::Base.view_paths = "./views/"一起使用对我有用。

0

我遇到了类似的问题,正在使用ActionMailer 3.0.3。在ActionMailer 3中不存在register_template_extension。

我正在使用Sinatra。我在APP_ROOT/lib中有mailer.rb(如下所示),而视图位于APP_ROOT/views/mailer中。这将发送一封带有主题的电子邮件,但正文为空。

require 'action_mailer'
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.view_paths = File.dirname(__FILE__)+"/../views/"
ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => 'exmaple.com',
  :user_name            => 'user@exmaple.com',
  :password             => 'password',
  :authentication       => 'plain',
  :enable_starttls_auto => true  }

class Mailer < ActionMailer::Base

   def new_comment_notifier(post,comment)
      @post = post
      @comment = comment

      mail(:to => "user@example.com",
           :subject => "new comment on: #{post.title}")
   end
end

可能存在一个问题与Rails 3相关。 - Joseph

0

在 Rails 中不是必需的,但是由于你在没有使用 Rails 的情况下使用 ActionMailer -- 你是否指定了ActionMailer::Base.register_template_extension('haml')


2
正如@Joseph所提到的,ActionMailer::Base.register_template_extension('haml')在ActionMailer 3+中不存在。 - dunedain289
@dunedain289,是的,我现在也看到了。嗯...它曾经是一个解决方案。对于这种情况造成的混淆,我感到抱歉。 - Jochem Schulenklopper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接