我可以在另一个动作中调用一个动作(在 rails 控制器中)吗? [英] Can I call an action in another action (in a rails controller)?

查看:40
本文介绍了我可以在另一个动作中调用一个动作(在 rails 控制器中)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当控制器中的一个动作被调用时,我可以从那个动作中调用另一个动作吗?

When an action in a controller has been called, can I then call another action from that action?

如果两个动作都有一些模板要渲染,会发生什么?

And what would happen if both actions have some template to render?

推荐答案

可以,如果它在同一个控制器中.

Yes you can, if it is in the same controller.

调用zoo 将为zoo 提供带有@x@a 实例的模板.foo 或 bar 都不会被渲染.如果您明确设置了 render 方法,那么您可能会收到双重渲染错误,除非您在调用第二个渲染之前return.

Calling zoo will provide the template for zoo with instances for @x and @a. Neither foo or bar will be rendered. If you have explicitly set a render method, then you might get a double render error, unless you return before the second render is called.

def foo
  @x = 1
end

def bar
  @a = 2
end

def zoo
  foo
  bar
end

这篇关于我可以在另一个动作中调用一个动作(在 rails 控制器中)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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