1

I have a functionality that doesn't relate to incoming requests (like get,post,etc).

But I want to follow MVC convention and make it like Model-Controller:

  • in controller part I will process income requests from another parts, decide from which model it should retrieve information, authorization, filtering and so on;

  • in model part I will store information, validate it, expire it and etc.

In Rails the controller's actions can be called by income request directed by routes file.

The problem for me that if I've created a controller like this:

class SomeController < ApplicationController

  def some_action; end

end

How can I call method some_action from SomeController from any place in my application?

P.S. Calling SomeController.new.some_action doesn't seem right to me, because I think all controllers are parts of app object.

megas
  • 21,401
  • 12
  • 79
  • 130

2 Answers2

0

You can do this by instanciating the controller.

SomeController.new.some_action

However this is not really an MVC way. I don't really know what you want to use it for, but probably there is a better way. For example it can be done as in the model, as the modification of data should belong there not to a controller.

Matzi
  • 13,770
  • 4
  • 33
  • 50
  • It doesn't seem right to me, all controllers are part of app object, the question how to call it. – megas Jan 03 '13 at 11:15
  • I wrote how you call it if you like. – Matzi Jan 03 '13 at 11:17
  • What is the reason to create object while at this time we have already instantiated object somewhere in app? – megas Jan 03 '13 at 11:20
  • 1
    The reason is that you don't have it. Each time a request comes, the appropriate controller is instantiated. All the others are not. – Matzi Jan 03 '13 at 11:23
  • Hm, when I'm declaring a controller method as a helper by helper_method, does it create an appropriate controller every time when I call this helper method? – megas Jan 03 '13 at 12:29
  • 2
    You can only invoke those helper methods when that controller is invoked by a request. If you have `helper_method` inside `FooController`, and your current request is routed to `BarController`, the helper methods from `FooController` will not be available. There *is no instance* of `FooController` automatically created. – user229044 Jan 03 '13 at 14:27
0

I think you should probably create a PORO which will encapsulate this functionality in some method. So that any logic dependent functionality should be within that instead of in the controller. Then you can call them in either controller.

bullfrog
  • 1,613
  • 1
  • 12
  • 13