Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a method of another controller

I have controllers named Children and Father and Mother where I need to call a method of FatherController and MotherController from ChildrenController.

I need to pass a JSON data to the both controller(not at the same request) get_details method from set_details method of ChildrenController. I am going to call any of the controllers method based on some conditions.

There is no route for get_details method in both controllers. I don't need any helper methods to write.

I need to call more than one Controller's method, not by Inheriting.

Father controller

class FatherController < ApplicationController

  def get_details(data)
    ##
    ## I need to do some operation with the 'data' received from children controller.
  end

end

Mother controller

class MotherController < ApplicationController

  def get_details(data)
    ##
    ## I need to do some operation with the 'data' received from children controller.
  end

end

Children controller

class ChildrenController < ApplicationController

  data = {
      "id":"2",
      "expiry_date":"25-09-2016"
  }.as_json

  def set_details        
    ## get_details(data) -> FatherController
    ## get_details(data) -> MotherController
  end

end

Please help how to do this or suggest me if there is any other way to do this.

Thanks.

like image 632
Gokul Avatar asked Jun 10 '16 13:06

Gokul


1 Answers

Easy. Make the method .self

class MotherController < ApplicationController
  def self.get_details(data)
  end
end 

And then:

class ChildrenController < ApplicationController
  def set_details        
    MotherController.get_details(data)
  end
end
like image 82
Ruslan Avatar answered Oct 08 '22 13:10

Ruslan