Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails controller lifecycle

Tags:

I want to know a little about the object lifecyle in rails, can I count on new instance per request?, Is it true to say that each web request creates a new controller instance?(by default)

like image 772
Chen Kinnrot Avatar asked Jul 12 '12 09:07

Chen Kinnrot


People also ask

What is request life cycle in Rails?

Web server receives request, analyses it and passes to application server. Application server calls up proper controller-action after talking with router. The resulted output is sent back to browser and displayed on screen.

What are the controller actions in Rails?

In the Rails architecture, Action Controller receives incoming requests and hands off each request to a particular action. Action Controller is tightly integrated with Action View; together they form Action Pack. Action Controllers, or just “controllers,” are classes that inherit from ActionController::Base .

How a request is processed in Rails?

After the DNS gets resolved, the request hits a web server, which asks Rails what it has for that url . Rails goes to the routes. rb file first, which takes the URL and calls a corresponding controller action. The controller goes and gets whatever stuff it needs from the database using the relevant model .

Does every model need a controller Rails?

Do I need a controller for each model? No, not necessarily. However, having one controller per RESTful resource is a convention for a reason, and you should carefully analyze why that convention isn't meeting your needs before doing something completely different.


1 Answers

Yes, you can rely on a fresh controller instance per request.

RailsGuides:

When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the method with the same name as the action.

like image 137
mahemoff Avatar answered Oct 11 '22 02:10

mahemoff