Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct way to run one controller action from another controller action without an HTTP redirect?

I'd like to be able to dispatch from one controller action to another conditionally, based on a combination of query parameters and data in the database.

What I have right now is something like:

class OldController < ApplicationController
  def old_controller_action
    if should_use_new_controller
      new_params = params.dup
      new_params[:controller] = "new_controller_action"
      redirect_to new_params
      return
    end
    # rest of old and busted
  end
end

class NewController < ApplicationController
  def new_controller_action
    # new hotness
  end
end

This works just fine, but it issues an HTTP redirect, which is slow. I'd like to be able to do this same thing, but within the same HTTP request.

Is there a clean way to do this?

Edit: The bounty will go to someone who can show me a clean way to do this that leaves the controllers and their actions relatively untouched (other than the redirect code itself).

like image 439
Scotty Allen Avatar asked Apr 30 '09 20:04

Scotty Allen


People also ask

What is the default action method for every controller in routeconfig?

Every controller can have a default action method as per the configured route in the RouteConfig class. By default, the Index () method is a default action method for any controller, as per configured default root, as shown below. However, you can change the default action name as per your requirement in the RouteConfig class.

How to name action methods in the web API controller?

As mentioned above, name of the action methods in the Web API controller plays an important role. Action method name can be the same as HTTP verbs like Get, Post, Put, Patch or Delete as shown in the Web API Controller example above.

What are action methods of controller class?

All the public methods of the Controller class are called Action methods. They are like any other normal methods with the following restrictions: Action method must be public. It cannot be private or protected Action method cannot be a static method.

What are the restrictions of action method in controller?

All the public methods of a Controller class are called Action methods. They are like any other normal methods with the following restrictions: Action method must be public. It cannot be private or protected. Action method cannot be overloaded. Action method cannot be a static method.


2 Answers

Instead of calling code across actions, extract the code to lib/ or something, and call that code from both controllers.

# lib/foo.rb
module Foo
  def self.bar
  # ...
  end
end

# posts_controller
def index
  Foo.bar
end

# things_controller
def index
  Foo.bar
end
like image 98
August Lilleaas Avatar answered Oct 15 '22 17:10

August Lilleaas


Create an instance of the controller class:

@my_other_controller = MyOtherController.new

Then call methods on it:

@my_other_controller.some_method(params[:id])

I prefer the module idea, but this should do the trick.

You can also pass parameters as a whole from another controller:

@my_other_controller.params = params
like image 28
Jarrod Avatar answered Oct 15 '22 16:10

Jarrod