Calling Controller methods from application controllers

Is there a way/workaround to call a controller method from application
controller?

#Controller
SomeController < ApplicationController

def some_method

end

end

#Application Controller
ApplicationController < ActionController::Base

def another_method

Call the SomeController.some_method from here

end

end

On 24 Apr 2008, at 09:47, Buddhi De silva wrote:

Is there a way/workaround to call a controller method from application
controller?

If you’re asking that question, chances are you’re doing something wrong

That said if you have

ApplicationController < ActionController::Base
def another_method
some_method
end
end

then you it will work as long as your controller has a method called
some_method

Fred

Frederick C. wrote:

Is there a way/workaround to call a controller method from application
controller?

If you’re asking that question, chances are you’re doing something wrong

That said if you have

ApplicationController < ActionController::Base
def another_method
some_method
end
end

then you it will work as long as your controller has a method called
some_method

Fred

Thanks for the quick reply Fred. But my issue is that some_method is in
SomeController, not in application controller. And i need to call
specific functions in specific controllers. like SomeOneController.new,
SomeTwoController.new, SomeOneController.some_method,…

On 24 Apr 2008, at 11:03, Buddhi De silva wrote:

That said if you have
Fred

Thanks for the quick reply Fred. But my issue is that some_method is
in
SomeController, not in application controller. And i need to call
specific functions in specific controllers. like
SomeOneController.new,
SomeTwoController.new, SomeOneController.some_method,…

Then you’re handling this the wrong way. The common code should be in
a module included in all the relevant controllers or pushed down into
the model.

Fred

Frederick C. wrote:

On 24 Apr 2008, at 11:03, Buddhi De silva wrote:

That said if you have
Fred

Thanks for the quick reply Fred. But my issue is that some_method is
in
SomeController, not in application controller. And i need to call
specific functions in specific controllers. like
SomeOneController.new,
SomeTwoController.new, SomeOneController.some_method,…

Then you’re handling this the wrong way. The common code should be in
a module included in all the relevant controllers or pushed down into
the model.

Fred

Common code is the code used for calling the some_method, its in the
ApplicationController. What i need is to call specific methods in
specific controllers.

This is the scenario to explain.

  1. User logs in (there are ajax based dashboard widgets that can be
    updated by user)

  2. If user clicks on a widget after the session timed out, the ajax
    based login window will pop up (similar to light box) for user
    verification. There is a before_filter for checking timeout and it
    render the login window.

  3. After verifying the user, login window will be closed

  4. Ajax dashboard widget will be updated

However this 4th step is not happening if ajax login window is shown and
user is verified.

My idea is to capture and save the original request, because the filter
chain get halted at the point of before_filter render the login window
(4th step is not happening as the login window closes). My plan is to
save the request info and then call the specific method in specific
controller based on that saved original request info. There would be
many widgets that use the ajax login window… so I need to keep this
DRY…

Any suggestion/help welcome…!

David A. Black wrote:

Hi –
See Fred’s answer, to which I’ll add: I think there’s some confusion
about how method-calling works. If you really mean SOC.some_method,
then you’re talking about a class method:

class SomeOneController
def self.some_method
# etc.

rather than an instance method. If you define an instance method
called some_method in SomeOneController, then it’s not a matter of
calling it from other controllers; it’s a matter of whether or not you
have an object in scope that can execute that method. (Actually that’s
always the question. With class methods, the necessary object happens
to be a class object.)

David

David, I’m a noob to ruby, sorry if i’m not asking the correct question.
The method is not a class method. The method would be a normal method we
find in any controller new, index, create… etc. is it possible for me
to access that method from ApplicationController.

I just posted SomeController.some_method they way i saw it. I just need
to find a workaround similar to that. My 3rd post explains what i need
to use this functionality for…

Hi –

On Thu, 24 Apr 2008, Buddhi De silva wrote:

ApplicationController < ActionController::Base

Thanks for the quick reply Fred. But my issue is that some_method is in
SomeController, not in application controller. And i need to call
specific functions in specific controllers. like SomeOneController.new,
SomeTwoController.new, SomeOneController.some_method,…

See Fred’s answer, to which I’ll add: I think there’s some confusion
about how method-calling works. If you really mean SOC.some_method,
then you’re talking about a class method:

class SomeOneController
def self.some_method
# etc.

rather than an instance method. If you define an instance method
called some_method in SomeOneController, then it’s not a matter of
calling it from other controllers; it’s a matter of whether or not you
have an object in scope that can execute that method. (Actually that’s
always the question. With class methods, the necessary object happens
to be a class object.)

David


Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!

2 things for you.

1.) The guys above give good advice. However, to solve your exact
problem, just add a blank definition of some_method in
ApplicationController, like so:

ApplicationController < ActionController::Base

def some_method
end

you already had this another_method in here, so I’m leaving it in

def another_method
some_method
end
end

When you call some_method from another_method, it will find your
inherited some_method implementation in SomeController and use it.

2.) When communicating class methods versus instance methods, in Ruby,
you want to indicate a class method like so:

ClassName.some_class_method

and an instance method like this:

ClassName#some_instance_method

Just FYI.

Best,

On Apr 24, 5:50 am, Buddhi De silva [email protected]

Brian D. wrote:

2.) When communicating class methods versus instance methods, in Ruby,
you want to indicate a class method like so:

ClassName.some_class_method

and an instance method like this:

ClassName#some_instance_method

Just FYI.

Best,

On Apr 24, 5:50�am, Buddhi De silva [email protected]

Hi Brian,

the “#” in ClassName#some_instance_method is treated as a comment

Any ideas?

Brian D. wrote:

2 things for you.

1.) The guys above give good advice. However, to solve your exact
problem, just add a blank definition of some_method in
ApplicationController, like so:

ApplicationController < ActionController::Base

def some_method
end

you already had this another_method in here, so I’m leaving it in

def another_method
some_method
end
end

When you call some_method from another_method, it will find your
inherited some_method implementation in SomeController and use it.

2.) When communicating class methods versus instance methods, in Ruby,
you want to indicate a class method like so:

ClassName.some_class_method

and an instance method like this:

ClassName#some_instance_method

Just FYI.

Best,

On Apr 24, 5:50�am, Buddhi De silva [email protected]

Thanka Brian, highly appreciate your advice… I think latter one could
do what i need to do… Fred and David thanks heaps for your advice as
well…

Hope this will work… i’ll keep posted on progress

  • buddhi

I think the easier way is going to be a simple redirection after step
3 is completed. Save the params hash on the session and redirect the
user to the Controller#action he requested when the session was timed
out. Then you could setup another before_filter that looks for a
params hash on the session, and if found, loads that data onto the
current params hash. This will incur a regular request instead of
Ajax, but I think it is the best way to do it.

On Apr 24, 9:01 am, Nathan E. <rails-mailing-l…@andreas-

You can’t actually write:

ClassName#some_instance_method

in your code. If you want to use an instance method, you need to call it
on an instance of the class, not the class itself. Consider a simple
example.

class SomeClass
def self.some_class_method

end

def some_instance_method

end
end

If I want to call the class method then I would type:

SomeClass.some_class_method

but to use an instance method, you need to call the method on an
instance of the class.

For example you might do

s = SomeClass.new
s.some_instance_method

I realize this doesn’t translate very well to controllers although I
have to be honest the requirement tells me that you are going about it
the wrong way. You should never have to invoke methods from multiple
other controllers actions from within the application controller at
once.

Buddhi,

The ‘#’ is for when you are trying communicate an instance method.
Not when coding it. You’ll see it all the time in Ruby documentation.

Again, to answer your original question, if you still have your code
in the same state as when you created this post, just add a blank
some_method instance method to your ApplicationController.

def some_method
end

That’s it.

On Apr 24, 10:54Â am, Buddhi De silva <rails-mailing-l…@andreas-

I’m still stuck with the same issue and it seems I’ve to accept what
rails/ruby has set as rules/limitations… hoping those are there for our
own good… :slight_smile:

Q1. However I have another question if all can kindly share the way they
would solve the issue…

Scenario:

  1. User logs in (there are ajax based dashboard widgets that can be
    updated by user)

  2. If user clicks on a widget after the session timed out, the ajax
    based login window will pop up (similar to light box) for user
    verification. There is a before_filter for checking timeout and it
    render the login window.

  3. After verifying the user, login window will be closed

  4. Ajax dashboard widget will be updated

However this 4th step is not happening if ajax login window is shown and
user is verified… because the filter chain get halted at the point of
before_filter render the login window (4th step is not happening as the
login window closes)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs