2

When we navigate through pages in a rails app, inturn we call one of the functions defined in the controller class. Lets say we access localhost:3000/articles/new then new action (method) of the ArticlesController class is called/invoked.It's simple.

But what i can't figure out is that since ArticlesController class is a pure Ruby class with some methods and we need an instance of this class to call one of it's methods. But we never explicitly do that. Then how the function call of any controllerclass is made possible ?

amritesh
  • 83
  • 6
  • That's kind of the point of something like Rails-to remove the need for us to manually instantiate everything our app needs. In any case, the framework instantiates action instances based on routing, which uses convention to determine the full class name. The request processing pipeline is responsible for instantiating the appropriate class and calling the appropriate method. – Dave Newton May 04 '15 at 17:01

2 Answers2

1

The RouteSet generates instances of any controller on demand based on the needs of the ActionDispatch routing system. See here for how this is done.

So unless you are testing a controller directly, you can rely on the router to supply you with a controller instance. And if you are testing one directly, you should be using an ActiveController::TestCase to do this work for you.

PinnyM
  • 35,165
  • 3
  • 73
  • 81
1

The controller is initialized automatically by rails. Specifically, this calls the action method on the controller class, which does the actual initialization.

stoodfarback
  • 1,299
  • 9
  • 12