Is there a way to redirect from one controller action to another without a server trip?

Hi

I want one controller action to invoke another action in another
controller, but I don’t want the overhead of a redirect_to response
which then generates another request to the correct action and
controller. Any suggestions?

Pieter

Create object for that controller then call.

class A
def a1

  • invoke=B.new**
    invoke.action(args)*
    end
    end

class B
def action(args)
something
end
end

regards,
Loganathan

something like

render :controller => othercontroller, :action => :otheraction, :params
=> params

?

(preserving the params would be a sweet bonus :slight_smile:

On Thu, Mar 8, 2012 at 5:25 PM, Pieter H. removed_email_address@domain.invalid
wrote:

something like

render :controller => othercontroller, :action => :otheraction, :params
=> params

  • Nope, I hope you cant call another controller action like this.*

Without thinking too much about it, how about if you create another
class that both your controllers can use?

This class processes your actions

class YourActionClass
def your_action(params)
# here you process your request
end
end

This is the controller you want to process the “redirect”.

class ControllerA < ApplicationController
def your_action
your_action_instance = YourActionClass.new
your_action_instance.your_action(your_parameters)
end
end

This is the controller you want to recognize your special cases and

redirect them.
class ControllerB < ApplicationController
def action
if special_case
your_action_instance = YourActionClass.new
your_action_instance.your_action(your_parameters)
# maybe you can render something here?
else
# your regular stuff goes here.
end
end
end

Thanks for the comments guys.

If there is not wasy way to bounce to another controller/action then
there should be. Its available via redirect_to, and one can render the
partials of another controllers actions, but it seems one cant quite
just get the action code to execute.

My usage case is this:

I am writing a web based simple text chat app. People submit comments,
calling the AddComment action, which updates the conversation, and
renders the comment to the people having the conversations.

I want to build in some predefined keywords that will get recognised in
the AddComment action, but will call another controller. Eg I have a
documents controller with a AttachDoc action which gets called normally
when the user clicks on the right button.

I now want to allow doc attachment from the commandline - So when
someone types ‘AttachDoc’ as a comment, controll should jump to the
documentscontroller. Redirect_to will do the job, but with a unnecessary
trip back to the user.

Still reading? thanks! Any suggestion?