Total Blog Views: 113
Blog Status: publish
Created By: swaz_ahmed Created at: 06-25-2024
When ever you visit any website or clicks any page you get the beautiful view with the respective contents. But do you wonder , how this happens there is so much happening behind and one of them is request, response cycle so, in this blog we will dive into the concept of request response cycle in rails.
So it is interaction between client and server so here the one who is interacting with website is client (you) and other is server.
1) Sending request through browser
When you visit any website like https://localhost:3000/articles or search anything on browser, it means you are sending request to the server. This request is called as http request ( HyperText Transfer Protocol). This request includes:
so for above request we made , https://localhost:3000/article this link is url, and its http method will be get , headers may contain infomation about cookies, tokens, etc, as it is get request so there will be no body.
2) DNS resolution
Now is request is sent to server but to start communication with server, browser first make DNS lookup to find where this site is hosted, means it will try to find IP address of the web server that could answe the request.
more simpley, the browser contacts a DNS server to resolve the human-readable domain name (e.g., example.com
) into an IP address, which is the address of the server that hosts the website.
3) Server recieves the request
The server with the resolved IP address receives the request. In the context of a Rails application, this server typically runs a web server software like Puma, Unicorn, or Passenger, which interfaces between the internet and your rails application.
4) Request at server side
So after the request is deliverd to sever, it usually passed to "web server" then to Rack middleware and finally to your rails aap.
5) Web server
web server is program that takes a request to your website from a user and does some processing on it. Their job is to understand request and make decision of how to deal with it.
6) Rack middleware
The web server pass the information to Rack. Rack is something like simple API to talk between Rails and web server. Rack tells the rails app that I have this request with data like http verb, url, header, etc and listen to rails and transfer the result back to web server with response with status code and body.
7) Routing
When rack pass the request to the rails application, firslty it goes to router. Router is first component of Rails application that handles the request. The router matches the URL to a route in the config/routes.rb
file and determines which controller and action should handle the request.
get '/articles', to: 'articles#index'
8) Controller action
Once the route is determined, Rails instantiates the corresponding controller and calls the specified action. Perform the code written in action.
consider following example
class ArticlesController < ApplicationController def index @articles = Article.all render 'index' end end
9) Model interaction
If the action requires data from the database, the controller interacts with the model. In the example above, the Article
model is queried to fetch all articles. Models in Rails use Active Record to communicate with the database.
10) View rendering
After the controller action gathers the necessary data, it renders a view. Views are templates that combine HTML with embedded Ruby (.erb
files) to dynamically generate HTML content.
11)Response The rendered HTML content is then packaged into an HTTP response, which includes:
12) Sending response back to server
after the request completes it life cycle in our app the response is sent to Rack and Rack to web server and then web server to your browser.
13) Browser renders the response
The browser receives the HTTP response, interprets the headers and the body, and renders the HTML content for you to view.
This is the whole procedure that take place when you perform certain action on any rails application website. I hope you understand this request response life cycle in rails.
Keep learning, Keep coding!
we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.
© Copyright Shadbox. All Rights Reserved
Rate Blog :
Share on :
Do you have any blog suggestion? please click on the link