10

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.

Gokul
  • 3,101
  • 4
  • 27
  • 45
  • 7
    The controller layer is likely not where you want this logic to live. You might want to consider pushing this to the model / business logic layer rather than trying to pass data around from controller to controller. For instance, create a plain-old Ruby object that knows how to handle the logic and will return the data that you need. `details = DomainObject.new(data).process` Within that DomainObject you can do whatever you need to pull out the data you want. – Carlos Ramirez III Jun 10 '16 at 14:17
  • 1
    I agree with @CarlosRamirezIII that this probably belongs in a model. But if you really want it in the controller you might try making a `Concern` out of the common method and including it in each controller that needs that method. More info on Concerns can be found here: http://stackoverflow.com/questions/14541823/how-to-use-concerns-in-rails-4 – Dan Jun 10 '16 at 15:49
  • 1
    @Dan I agree with you. Thanks for your comment. – Gokul Jun 17 '16 at 09:29

3 Answers3

20

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
Ruslan
  • 1,919
  • 21
  • 42
  • 2
    dont works with rail 5+ : `uninitialized constant ChildrenController::MotherController ` ... – Matrix Oct 15 '19 at 13:35
3

Either remove this logic from the controller or define it in the ApplicationController, of which all your controllers are inheriting from.

2

Why dont you simply write your function or method into the MODEL

class MotherModel < ApplicationRecord

     def self.mothermodel_method
     end
end


class ChildController < ApplicationController
     def access_mother_method
         @result_from_mother_method = MotherModel.mothermodel_method
     end
end
pensebien
  • 506
  • 4
  • 16
  • 1
    You can add relationships to allow access to the child model from the mother model directly and do any manipulation you so wish on the child model – pensebien Jul 26 '17 at 00:06