views:

54

answers:

2

Hi,

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

+2  A: 

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'
EmFi
But this will send the HTTP state for a redirect. This is not what I am looking for.
brainfck
+2  A: 

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.

Damien MATHIEU