1

I want to call a method of one controller in another controller's method, and also pass parameters like below:

Controller A:

@var
def methodA
  update(@var)
end

Controller B:

def update(var)
  var1 = var
end

Is there any way to do this?

sawa
  • 165,429
  • 45
  • 277
  • 381
Moriarty
  • 159
  • 2
  • 16
  • Since `update` is an instance method, you should have an instance of `B`, on which you want to call `update`. And you should just explicitly call the method on it. But you have not presented that instance in the question. – sawa Nov 05 '18 at 07:52

1 Answers1

0

Why not define the shared method in ApplicationController instead and call it in both controllers as they each inherit it's methods. Like so:

ApplicationController:

class ApplicationController < ActionController::Base

  protected

  def update(var)
    var1 = var
  end
end

Other Controllers:

class SomeController < ApplicationController    
  def some_method()
    @var = 'something'
    update(@var)
  end
end


class SomeOtherController < ApplicationController    
   def some_method()
     @var = 'something'
     update(@var)
   end
 end
Simon L. Brazell
  • 1,132
  • 8
  • 14