36

If I've got a method in a different controller to the one I'm writing in, and I want to call that method, is it possible, or should I consider moving that method to a helper?

2
  • 1
    Could you add facts to the problem you want to solve by calling the method of another controller? Currently it is pretty vague, and the solution will depend on the problem.
    – mliebelt
    Nov 22, 2011 at 11:01
  • Indeed, please elaborate what you are trying to solve. what is the shared method doing?
    – nathanvda
    Nov 22, 2011 at 11:21

4 Answers 4

56

You could technically create an instance of the other controller and call methods on that, but it is tedious, error prone and highly not recommended.

If that function is common to both controllers, you should probably have it in ApplicationController or another superclass controller of your creation.

class ApplicationController < ActionController::Base
  def common_to_all_controllers
    # some code
  end
end

class SuperController < ApplicationController
  def common_to_some_controllers
    # some other code
  end
end

class MyController < SuperController
  # has access to common_to_all_controllers and common_to_some_controllers
end

class MyOtherController < ApplicationController
  # has access to common_to_all_controllers only
end

Yet another way to do it as jimworm suggested, is to use a module for the common functionality.

# lib/common_stuff.rb
module CommonStuff
  def common_thing
    # code
  end
end

# app/controllers/my_controller.rb
require 'common_stuff'
class MyController < ApplicationController
  include CommonStuff
  # has access to common_thing
end
3
  • include GoodSuggestion Thanks @jimworm
    – edgerunner
    Nov 22, 2011 at 11:07
  • 1
    Creating an instance of the other controller???? Arrrrrrrgggh (indeed). Another option, since the question is so vague: move code to models.
    – nathanvda
    Nov 22, 2011 at 11:23
  • Since there aren't any details on the question(yet) I'm assuming that @cjm2671 is asking for controller specific functionality. Of course, if it is business logic, it must be in the models :)
    – edgerunner
    Nov 22, 2011 at 11:30
3

Try and progressively move you methods to your models, if they don't apply to a model then a helper and if it still needs to be accessed elsewhere put in the ApplicationController

5
  • 1
    I would never move code from a controller to a helper, rather a module living in /lib. Only view-code should live in a helper.
    – nathanvda
    Nov 22, 2011 at 11:24
  • 1
    putting something in a helper is only short lived. you'll get to the point where you don't have to put in a lib/ throwing everything into a module isn't too good either. Nov 22, 2011 at 11:29
  • 1
    I prefer using modules over helpers, because they are true OO, i can include/extend them at will. I can group stuff together that has actual meaning. Helpers are just buckets. I recommend to only use those for view-related methods. Not sure what you mean with the you'll get to the point .....
    – nathanvda
    Nov 22, 2011 at 11:35
  • 1
    true OO is putting method in the model. you access them from a controller or view, but obviously you can have them in a module if that function is common and access what you can through a model when ever you can. Nov 22, 2011 at 11:40
  • There is also functionality that does not belong in a model, but in its own class or module. E.g. an implementation of decryption algorithm, sending sms, ... those belong in lib (or make a gem).
    – nathanvda
    Nov 22, 2011 at 12:13
0

If you requirement has to Do with some DB operations, then you can write a common function (class method) inside that Model. Functions defined inside model are accessible across to all the controllers. But this solution does to apply to all cases.

0

I don't know any details of your problem, but maybe paths could be solution in your case (especially if its RESTful action).

http://guides.rubyonrails.org/routing.html#path-and-url-helpers

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.