Ruby On Rails M-V-C Architecture - Veltris

Ruby On Rails M-V-C Architecture

September 28, 2020 | Apeksha singh

    Ruby on rails is an open source web framework written in the Ruby programming language, and all the applications in Rails are written in Ruby. It is very popular framework for web development due to its amazing features. This framework has built-in solutions to many common problems that developer face during web development.

    mvc architecture

    Ruby on Rails uses the Model-View-Controller (MVC) architectural pattern. MVC is a pattern for the architecture of a software application. It separates an application into the following components:

    • Models for handling data and business logic
    • Controllers for handling the user interface and application
    • Views for handling graphical user interface objects and presentation

    This separation results in user requests being processed as follows:

    1. The browser sends a request for a page to the controller on the server
    2. The controller retrieves the data it needs from the model in order to respond to the request
    3. The controller gives the retrieved data to the view
    4. The view is rendered and sent back to the client for the browser to display

    As you can see, each component of the model-view-controller architecture has its place within the app directory—the models, views, and controllers sub directories respectively.

    MODELS
    ActiveRecord is the module for handling business logic and database communication. It plays the role of model in our MVC architecture.

    Lets consider this example where a user can create articles and each article can have many comments. The image on the left shows the all the validations and associations of article model with other models, where as the image on the write shows the schema of articles.

    ActiveRecord is designed to handle all of an application’s tasks that relate to the database, including:

    1. establishing a connection to the database server
    2. retrieving data from a table
    3. storing new data in the database.

    CONTROLLERS
    The ActionController is a module which handles the application logic of your program, acting as glue between the application’s data, the presentation layer, and the web browser.

    The following images are example of how controllers are created for a model. Here I have created controllers for articles model.

    In this role, a controller performs a number of tasks including:

    1. deciding how to handle a particular request.
    2. retrieving data from the model to be passed to the view
    3. gathering information from a browser request and using it to create or update data in the model.

    VIEWS
    The principle of MVC is that a view should contain presentation logic only. This principle holds that the code in a view should only perform actions that relate to displaying pages in the application; none of the code in a view should perform any complicated application logic, nor store or retrieve any data from the database. In Rails, everything that is sent to the web browser is handled by a view.

    A view need not actually contain any Ruby code at all — it may be the case that one of your views is a simple HTML file; however, it’s more likely that your views will contain a combination of HTML and Ruby code, making the page more dynamic. The Ruby code is embedded in HTML using embedded Ruby syntax.

    This views generates a list of all the articles created by user.

    If you liked this blog, please click the 👏 button and share to help others find it! Feel free to leave a comment below.

    Related Articles