DEV Community

Mohammed Shaikh
Mohammed Shaikh

Posted on


 What is M.V.C?
MVC (Model-View-Controller) is a way to structure a web-app folder where the model deals with the backend, the view deals with the front end, and the controller deals with connecting everything. A common example that is given for MVC is the restaurant example. The chef (Model) creates food (instances of a class) and manages the inventory (database). The waiter (Controller) will take the order (request) from the customer (View) and take it to the chef (Model) and then it will take the order (request) back to the customer (View). The customer (View) can make the order (request).

 What is Model?
Model is the data management system of the program. Model also will contain the majority of the logic. The rails command to generate a model would be:

rails g model (Model name) (table column name):(column data type)

If we were to code the MVC example, we would do

rails g model Restaurant name:string address:string

.The above command would output

invoke  active_record
create    db/migrate/20191216200109_create_restaurants.rb
create    app/models/restaurant.rb
Enter fullscreen mode Exit fullscreen mode

The models/restaurant.rb file is where your restaurant class will go and the db/migrate/20191216200109_create_restaurants.rb file is where your data table goes. Combined, they make the model in MVC. For more information on generators

 What is the view?
View is the frontend part of your code that will contain code that generates what your user will see. If we continue the example from above, we could put:

create  app/controllers/restaurants_controller.rb
route  get 'restaurants/new'
get 'restaurants/create'
get 'restaurants/edit'
get 'restaurants/update'
get 'restaurants/destroy'
get 'restaurants/index'
get 'restaurants/show'
      invoke  erb
      create    app/views/restaurants
      create    app/views/restaurants/new.html.erb
      create    app/views/restaurants/create.html.erb
      create    app/views/restaurants/edit.html.erb
      create    app/views/restaurants/update.html.erb
      create    app/views/restaurants/destroy.html.erb
      create    app/views/restaurants/index.html.erb
      create    app/views/restaurants/show.html.erb
      invoke  helper
      create    app/helpers/restaurants_helper.rb
      invoke  assets
      invoke    scss
      create      app/assets/stylesheets/restaurants.scss
Enter fullscreen mode Exit fullscreen mode

The controller command creates the 7 RESTful routes for restaurant and creates a view file and a controller method for each route. It is a very useful tool. There is one drawback to it, though. It creates a view file for create, update, and destroy. As soon as the command creates those files, you may have to delete it.

 What is the controller?
Controller is the code that connects both side, Model and View. When the request is made by browser, it first goes to the Controller, then it is sent to Model or View depending on what is needed for that request. The above command created the Controller for us already.

class RestaurantsController < ApplicationController
  def new

  def create

  def edit

  def update

  def destroy

  def index

  def show
Enter fullscreen mode Exit fullscreen mode

When the user searches a url on our website, it will come to one of these methods. For example, if the user searches the home page, it will go to the index method. The index method will take the request and do whatever logic we put in that method and then send it off to the Model if needed. if not, then it will send it off to the user the correct View file.

Model, View, and Controller is a way of applying the concept of separation of concerns, where every piece of code is just responsible for one thing. If there is a need to debug or update, we would know exactly where everything is, so it would make our lives easier for us. MVC also allows teams to work together on the same app without git conflicts

Top comments (0)