Calling a controller from another controller

How do I call the create method of b_controller from a_controller?

a couple things:

(A) if you want to specifically call the :create method for an object,
it doesn’t matter which controller u call it from (i.e,
ExampleClass.create… will work whatever controller u put it in) - so
if this is what u want, u’r done

===============

(B) if you want to call any method from one controller to the other,
consider the two possiblities:
(B1) heirarchy -
as all controllers inherit from ApplicationController, you can make a
controller inherit from a different controller, which will pass the
methods along the line of heirarchy, such as

Class ExampleController < OtherController

def a
b # will work
“apples”
end

end

Class OtherController < ApplicationController

def b
a # wont work
“bannanas”
end

end

===============

(B2) consider using a module; you can mix in methods from a module 

into both controllers, using require and inlcude respectively:

module MixMe # put this in the /lib folder

def a
“carrots”
end

def b
“dung”
end

end

require ‘mix_me’
Class ExampleController < ApplicationController

include MixMe # mixes in the module

def some_method
a # works
b # works
render_text “success…”
end

end

require ‘mix_me’
Class OtherController < ApplicationController

include MixMe # mixes in the same module

def some_other_method
a # works
b # works
render_text “success!”
end

end

and, walla - should work perfectly.

===============

pick u’r choose.
(D) good luck

:slight_smile:

shai

Hi, you can do the following:

class BController < ActionController::Base

def create( … )

end

end

class AController < ActionController::Base

def example_method
b_controller = BController.new
b_controller.create( … )
end

end

For additional examples of OOP in Ruby, please consult any of the
available
Ruby books.

Good luck,

-Conrad