在Rails的Controllers和Views中使用的Helper方法 [英] Helper methods to be used in Controllers AND Views in Rails

查看:116
本文介绍了在Rails的Controllers和Views中使用的Helper方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以在Rails的helper文件夹内的Helper类中放置一个辅助方法.然后可以在任何视图中使用该方法.而且我知道我可以将方法放在ApplicationController类中,并且该方法可以在任何控制器中使用.

I understand I can put a helper method in a Helper class inside the helper folder in Rails. Then that method can be used in any view. And I understand I can put methods in the ApplicationController class and that method can be used in any controller.

在哪里放置控制器和视图中经常使用的方法?

Where's the proper place to put a method that is frequently used in both controllers and views?

推荐答案

您可以将其放在控制器中并调用:

You can put it in the controller and call:

helper_method :my_method

控制器上的

.

from the controller.

这篇关于在Rails的Controllers和Views中使用的Helper方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆