【问题标题】:Make a custom helper available to both Mailer and View in Rails 3.1在 Rails 3.1 中为 Mailer 和 View 提供自定义助手
【发布时间】:2011-10-11 07:51:04
【问题描述】:

这是在 Rails 3.1 中为 Mailer 和视图提供帮助程序的最佳方式吗?

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

我试过了

helper :mailer

单独使用,但这不允许我使用 EventMailer 类中的帮助器。

我试过了

add_template_helper(MailerHelper)

但遇到了同样的问题。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 actionmailer helper mailer


    【解决方案1】:

    rails 助手应该是 view 助手。

    你会注意到下面的代码:

    class MyController < ApplicationController
        helper :my
    end
    

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

    总结:

    helper :my 并且您可以在视图中使用助手

    include MyHelper 你可以在你的控制器中使用助手

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

    class EventMailer < ActionMailer::Base
        include MailerHelper
        helper :mailer
    
        # rest of the code goes here ...
    end
    

    会做你想做的,并允许你在你的邮件和视图中使用你的助手。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 2014-02-07
      • 2016-01-29
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 2015-08-11
      相关资源
      最近更新 更多