Calling a controller from another

依然范特西╮ 提交于 2019-12-24 03:12:13

问题


I want to call an action of a controller from another controller.

How can I do this?

Some years ago, there were compononents, but they are not available any more.

Best regards


回答1:


You can't call an other controller's action method.
You have only two solutions.

Doing a redirect to the appropriate URL.

redirect_to '/'

But of course if you have datas from a form, you lose them.

Render the action from an other controller.

render 'controller/action'

You keep all your defined datas (params and everything).
But you need to do again all what the other controller would do.

One solution to avoid repeating code lines would be to have a library method defining every vars you need in your view and use this method in your two controllers.




回答2:


redirect_to is the preferred way of doing this.

If you just want to render the other action's view with the current action's logic you can pass the view as an option to render.

render 'other_controller/action'


来源:https://stackoverflow.com/questions/1833857/calling-a-controller-from-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!