02 MVC Pattern
- Model: Taking Care of Business
- View: Looking Good / Consists of partials
- Controller: Holding It All Together / Process ![[Screen Shot 2024-02-15 at 01.54.20.png]]
Advantages: 1. Easy to know where in the project to find relevant code 2. Allow for parallel development of the system 3. Smaller components are easier to test and debug 4. Allows for division of labor based on expertise 5. Easy to extend functionality of the system 6. Can reuse components in other applications
Journey of a request in Ruby on Rails web application¶
-
User Enters URL
- Uniform Resource Locator (网址): address of a given unique resource on the Web
- In Ruby, URL is a request to view a certain resource or perform an action on a web application built with Ruby on Rails
- structure of URL -- REST architecture
http[s]://www.example.com/controller/action/id- HTTP/HTTPS: This is the protocol used for the request. HTTPS is the secure version of HTTP and is recommended for all web applications.
- Domain: This is the domain name of the application (e.g.,
www.example.com). - Controller: In Rails, the controller is responsible for handling requests that come to the application. It's named after the resource it manages (e.g.,
articles,users). The controller name in the URL signifies which controller the routing system should dispatch the request to. - Action: The action represents a specific function within the controller. It defines what should be done with the request (e.g.,
show,edit,create). In a RESTful design, standard actions correspond to CRUD (Create, Read, Update, Delete) operations. - ID: This is an optional parameter, typically used to specify a particular resource instance. For example, in a URL like
/articles/show/1,1would be the ID of the article to be displayed by theshowaction.
- Nested resources
- For resources that are logically contained within another resource, Rails allows you to define nested routes. For example, if each article can have many comments, you might have a route like
/articles/:article_id/comments/:id
- For resources that are logically contained within another resource, Rails allows you to define nested routes. For example, if each article can have many comments, you might have a route like
- Query strings
https://www.example.com/articles?search=rails&sort=recent
-
Routing: Rails router
- Router: parse the URL and decide which controller and action (method) to dispatch the request to
- based on the routing rules defined in
config/routes.rb - RESTful Routes
- GET
/articles: Maps to theindexaction, displays a list of articles. - GET
/articles/new: Maps to thenewaction, returns a form for creating a new article. - POST
/articles: Maps to thecreateaction, creates a new article. - GET
/articles/:id: Maps to theshowaction, displays a specific article. - GET
/articles/:id/edit: Maps to theeditaction, returns a form for editing an article. - PATCH/PUT
/articles/:id: Maps to theupdateaction, updates a specific article. - DELETE
/articles/:id: Maps to thedestroyaction, deletes a specific article.
- GET
-
Dispatcher
- Technically, the Rails router itself acts as a dispatcher. Once it determines the appropriate controller and action, it forwards the request to that controller.
-
Controller
- It's responsible for making sense of the request, interacting with the model to retrieve, update, or create data, and then deciding how to respond. This could involve rendering a view or redirecting to another action based on business logic.
- Parameters: The controller receives parameters from the request (such as form data or query strings), which can influence how it interacts with the model or which view is rendered.
- Session and Cookies: The controller also has access to session and cookie data, enabling it to track user state across requests.
- It's responsible for making sense of the request, interacting with the model to retrieve, update, or create data, and then deciding how to respond. This could involve rendering a view or redirecting to another action based on business logic.
-
Model
- For resources that are logically contained within another resource, Rails allows you to define nested routes. For example, if each article can have many comments, you might have a route like
-
View
- Once the controller has decided on the response, it typically renders a view.
-
Response
- The compiled HTML page (or other responses like JSON or XML, depending on the request) is sent back through the controller to the user's browser.
-
Rendering or Redirecting
- If the controller decides to render a view, it combines the template with any instance variables to produce an HTML response.
- If it decides to redirect, the browser is instructed to make a new request to a different URL.