从邮件程序访问助手?

时间:2011-02-08 19:09:23

标签: ruby-on-rails ruby-on-rails-3

我尝试从rails 3邮件程序访问帮助程序方法,以便访问当前用户进行会话。

我把帮助器:应用程序放在我的邮件程序类中,这似乎有效,除了其中定义的方法不可用于我的邮件程序(我得到未定义的错误)。有谁知道这应该如何工作?

这是我的班级:

class CommentMailer < ActionMailer::Base
  default :from => "Andre Fournier <andre@gfournier.com>"

  helper :application
end

谢谢, 肖恩

11 个答案:

答案 0 :(得分:69)

要使您能够从ActionMailer视图访问应用程序帮助程序,请尝试添加以下内容:

add_template_helper(ApplicationHelper)

到您的ActionMailer(位于default :from行下方)。

答案 1 :(得分:46)

使用helper ApplicationHelper

class NotificationsMailer < ActionMailer::Base

  default from: "Community Point <Team@CommunityPoint.ca>"
  helper ApplicationHelper
  helper NotificationMailerHelper

  # ...other code...

注意:这些辅助方法仅适用于视图。它们在邮件程序类中不可用(在我的示例中为NotificationMailer)。

如果您在实际的邮件程序类中需要它们,请使用include ApplicationHelper,如下所示:

class NotificationMailer < ActionMailer::Base

  include ApplicationHelper

  # ... the rest of your mailer class.

end

来自this other SO question

答案 2 :(得分:24)

这是一个非常古老的问题,但我没有看到完整的答案,所以我会尝试,因为我没有找到其他资源。

这取决于您使用辅助模块中定义的方法执行的操作。如果它们是类方法,并且在特定实例上未调用的所有内容似乎都是3.2.13的类方法,则需要使用

extend ApplicationHelper

如果是实例方法

include ApplicationHelper

如果您想在邮件程序视图中使用它们

helper ApplicationHelper

答案 3 :(得分:8)

您可以尝试混合所需的辅助模块:

class CommentMailer < ActionMailer::Base
  include ApplicationHelper
end

答案 4 :(得分:3)

Josh Pinter的回答是正确的,但我发现没有必要。

必须正确命名帮助程序。

NotificationMailerHelper是正确的。 NotificationMailersHelper(注意s)不正确。

帮助程序的类和文件名必须匹配并拼写正确。

Rails 3.2.2

答案 5 :(得分:3)

include ActionView::Helpers::TextHelper在Mailer控制器(.rb文件)中为我工作。这允许我在Mailer控制器动作中使用复数帮助器(帮助程序在Mailer视图中的get go工作正常)。没有其他答案有效,至少在Rails 4.2上没有。

答案 6 :(得分:2)

如果你想从ActionMailer中调用helper方法,你需要在Mailer文件中包含helper(module),如果Helper模块名是“UserHelper”,那么需要在Mailer文件中编写以下内容

class CommentMailer < ActionMailer::Base
    default :from => "Andre Fournier <andre@gfournier.com>"
    add_template_helper(UserHelper)
end

或者

class CommentMailer < ActionMailer::Base
    default :from => "Andre Fournier <andre@gfournier.com>"
    include UserHelper
end

希望这有用。

答案 7 :(得分:2)

促进方法成为ApplicationController中可用帮助程序的单一方法版本也适用于ActionMailer:

class ApplicationMailer < ActionMailer::Base
  helper_method :marketing_host

  def marketing_host
    "marketing.yoursite.com"
  end
end

从那里,您可以从任何邮件程序视图中调用marketing_host

答案 8 :(得分:1)

我不确定你在这里做了什么,但是当我想从邮件程序访问current_user时,我制作了一个邮件方法,我将用户传递给参数:

class CommentMailer < ActionMailer::Base
  default :from => "Andre Fournier <andre@gfournier.com>"

  def blog_comment(user)
    @recipients                   = user.email
    @from                         = "andre@gfournier.com"
    @sent_on                      = Time.now
    @timestamp                    = Time.now
    @user                         = user
  end
end

通过上述内容,可以从邮件程序视图中访问@user以及所有其他实例变量./views/comment_mailer/blog_comment.html.erb和./views/comment_mailer/blog_comment.text.erb < / p>

另外,你可以制作一个叫做

的助手

comment_mailer_helper.rb

并将您想要提供给邮件程序视图的任何方法放入帮助程序。对于帮助者来说,这似乎更像你可能想要的东西,因为帮助器设计用于帮助视图,而邮件程序类似于控制器。

答案 9 :(得分:0)

默认情况下,电子邮件中没有*_path个助手可供访问。有必要使用所需助手的*_url形式。因此,例如,不必使用user_path(@user),而是使用user_url(@user)。 见Action Mailer basics

答案 10 :(得分:-4)

实现我想要的目标的一种hackish手段是将我需要的对象(current_user.name + current_user.email)存储在线程属性中,如:Thread.current[:name] = current_user.name。然后在我的邮件程序中,我只是将新的实例变量分配给线程中存储的值:@name = Thread.current[:name]。这是有效的,但如果使用延迟工作之类的东西,它就不会起作用。

相关问题