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)
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.
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 .
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With