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,...
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,...
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.
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..
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,...
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.)
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..
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:
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.
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.
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.
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..
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)