Rails 3:在控制器内部调用函数 [英] Rails 3: Call functions inside controllers

查看:40
本文介绍了Rails 3:在控制器内部调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想在控制器内部调用函数,我应该把它们放在哪里?

If I want to have functions to be called inside controllers, where should I put them?

推荐答案

如果您希望它位于控制器的本地,那么您需要做的就是将其添加到您希望使用的控制器中.

if you want it to be local to a controller then all you need to do is to add it to the controller you wish to use.

private
def myfunction
  function code.....
end

对于所有控制器,您可以将其放在应用程序控制器中,因为所有控制器都是子类.

to all controllers you can put it inside the application controller, because all controlers are sub classed.

protected

def myfunction

  function code.....

end

如果你想访问你的视图,那么你可以创建一个助手

If you want access in your views then you can create a helper

def myfunction

  function code...

end

这篇关于Rails 3:在控制器内部调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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