为Mailer和View in Rails 3.1提供自定义帮助程序

pin*_*ngu 16 ruby-on-rails actionmailer helper mailer ruby-on-rails-3

这是在Rails 3.1中为Mailer和View提供帮助的最佳方法吗?

class EventMailer < ActionMailer::Base
  include MailerHelper
  helper :mailer
Run Code Online (Sandbox Code Playgroud)

我试过了

helper :mailer
Run Code Online (Sandbox Code Playgroud)

本身,但这不允许我在EventMailer类中使用帮助器.

我试过了

add_template_helper(MailerHelper)
Run Code Online (Sandbox Code Playgroud)

但有同样的问题.

Ben*_*ret 33

轨道助手应该是视图助手.

您会注意到以下代码:

class MyController < ApplicationController
    helper :my
end
Run Code Online (Sandbox Code Playgroud)

将使方法MyHelper可用于视图,但不会使您的控制器操作.include MyHelper将使辅助方法在控制器中可用.

总结:

helper :my 并且您可以在视图中使用帮助程序

include MyHelper 并且您可以在控制器中使用帮助程序

我解释了一下,但你已经回答了你的问题:

class EventMailer < ActionMailer::Base
    include MailerHelper
    helper :mailer

    # rest of the code goes here ...
end
Run Code Online (Sandbox Code Playgroud)

会做你想做的事情,并允许你在你的邮件和你的意见中使用你的帮助.

希望这可以帮助.