0

我想知道如何使操作邮件程序可以使用帮助文件的功能?我有一个名为 UserMailer 的动作邮件程序和一个名为 sessions_helper.rb 的助手。如何使 UserMailer 可以使用这些方法?我试过“包括”,但它给出了以下错误

我试图获取的方法是“current_user”,我收到错误

undefined local variable or method `cookies' 

我正在使用 Rails 3.2.1

谢谢

4

3 回答 3

4

你可以像这样在你的邮件中提供帮助,

类通知器 < ActionMailer::Base

  add_template_helper(ApplicationHelper)

  #...

结尾

As far as current_user go, I don't think mailers have any concept of cookies as ActionController does. As a better design choice I'd keep my mailer independent of the current_user. Mailers are not concerned with who the current_user is ( similar to models ). For that matters, mailers are not even concerned with who the user is, they are concerned with "email, subject, and body".

So when calling mailer methods, you can pass them the user object ( it can be of the current_user or any body else ) so that they can get the email , generate the subject and the body.

于 2012-04-05T04:12:19.013 回答
2

我不得不为此添加一个文件config/initializers

class ActionMailer::Base
    helper MiscHelper
    helper ExtraMailHelper
end

我想您会根据需要为其他助手添加行,例如:

class ActionMailer::Base
    helper MiscHelper
    helper ExtraMailHelper
    helper SessionsHelper
end
于 2012-04-05T04:05:22.153 回答
1

If you want to include specific helpers in specific mailers you have, you can use this.

class RegistrationMailer < ActionMailer::Base

  include MyOwnHelper


  def method
  end
end
于 2012-04-05T06:14:10.533 回答