Make a custom helper available to both Mailer and View in Rails 3.1

烂漫一生 提交于 2019-12-03 15:43:38

问题


is this the best way to make a helper available to both Mailer and view in Rails 3.1?

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

I tried

helper :mailer

on its own, but that didn't allow me to use the helpers in the EventMailer class.

I tried

add_template_helper(MailerHelper)

but had the same problem.


回答1:


The rails helpers are supposed to be view helpers.

You will notice that the following code :

class MyController < ApplicationController
    helper :my
end

will make the methods in MyHelper available to the views, but not to your controller actions. include MyHelper will make the helper methods available in the controller.

Summarized :

helper :my and you can use the helpers in your views

include MyHelper and you can use the helpers in your controller

I explained a bit more, but you already answered your question :

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

    # rest of the code goes here ...
end

will do what you want and allow you to use your helper in both your mailer and your views.

Hope this helps.



来源:https://stackoverflow.com/questions/6795109/make-a-custom-helper-available-to-both-mailer-and-view-in-rails-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!