我可以在另一个动作中调用一个动作(在 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