使自定义助手可用于 Rails 3.1 中的 Mailer 和 View
这是在 Rails 3.1 中为 Mailer 和视图提供助手的最佳方式吗?
class EventMailer < ActionMailer::Base
include MailerHelper
helper :mailer
我
helper :mailer
自己尝试过,但这不允许我使用 EventMailer 类中的帮助器。
我尝试过
add_template_helper(MailerHelper)
但遇到了同样的问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails 助手应该是视图助手。
您会注意到以下代码:
将使
MyHelper
中的方法可用于视图,但不可用于您的控制器操作。include MyHelper
将使帮助器方法在控制器中可用。总结:
helper :my
并且您可以在视图中使用帮助器include MyHelper
并且您可以在控制器中使用帮助器我解释了更多,但您已经回答了您的问题问题:
将执行您想要的操作,并允许您在邮件程序和视图中使用助手。
希望这有帮助。
The rails helpers are supposed to be view helpers.
You will notice that the following code :
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 viewsinclude MyHelper
and you can use the helpers in your controllerI explained a bit more, but you already answered your question :
will do what you want and allow you to use your helper in both your mailer and your views.
Hope this helps.