Model–View–Controller (MVC) is a software pattern for implementing user interfaces. It divides a given software application into three interconnected parts to separate internal representations from how information is presented to or accepted by the user.
Whenever the user visits a website or interacts with a web application, clicks on a link, the browser sends an HTTP request to the server. The web applications Router file handles this request from the server. The Router acts as a sort of traffic controller for incoming HTTP requests. It decides which Controller to send the HTTP request to and in the route.rb file, each resource corresponds to a Controller.
Each Controller has at least one or more Actions. Each Action is essentially a Ruby method. The HTTP request includes which Action to execute within the Controller. If the code inside the Action needs information from the database, the request is then sent to the Controller’s Model. Any information that the Model gathers can now be stored in a variable and sent to the View. Each Action will redirect or render to the View. The redirect will send the user to an existing route, while rendering will open up a new page.
The Model helps your application interact with its database. It is essentially a translator between Ruby and SQL (the language of databases). The Controller’s request to access the database is passed through the Model and on to the database. The application’s data is stored in the database, and the SQL commands generated by the Model are implemented at this level. Those commands can add, modify, delete, or gather specific data that will eventually be displayed in the browser.
The View is where Ruby meets the frontend, such as HTML/CSS. The View can contain static or dynamic content and access data from the database stored in the Action variables. The View is the user interface it's what the users sees when they are interacting with the application. The View utilizes template engines such as embedded ruby which allows you to use erb tags to output variables or use logic such as if statements right in the view.
MVC is one of the most frequently used design patterns, by separating functionality, logic and the interface in an application. It allows developers to work on the same project with ease.