Ruby on Rails: MVC
What is MVC? First things first, it stands for Model View Controller. It refers to an architecture for creating web applications. In simpler terms, it’s used to help build frameworks for applications. Please keep in note, MVC is not specific to Ruby on Rails and I’ll be speaking about it in the context of Rails. Each one has its own separate responsibilities. One way that helped me understand the MVC framework is the restaurant analogy. Keep the image below in mind while I explain each one in detail!
Model
Think of the model(s) as the chef(s) in the restaurants. They represent the data and logic of the app. They’re responsible for manipulating and saving data and as a result, have a relationship with the database. In addition, they contain validations
, associations
with other models, and methods
that interact with the database. Look at the code below for example!
class Product < ApplicationRecord
has_many :cart_products
belongs_to :user
has_many :reviews
validates_presence_of :title, :price, :description, :image, :category def self.reviewed
self.joins(“INNER JOIN reviews ON reviews.product_id = products.id”)
endend
View
Think of views as the customer(s) in the restaurant analogy. It is responsible for rendering what the user sees and is what the user interacts with. In Ruby, the code is written in ERB
or Embedded RuBy. Look at the code below for example!
<div class=”product-show”>
<%= @product.title %>
$<%= @product.price %>
<%= @product.description %> <div class=”product-seller”>
Seller: <%= link_to @product.user.username, user_products_path(@product.user) %>
</div></div>
This would render something along the lines of this.
Straw Hat
$20
New
Seller: JW (imagine JW as a link!)
Controller
The controller is like the waiter in a restaurant. It connects the model and the routes. It only has access to the instance variables provided by the controller. If you remember the first image, the user essentially interacts with the view and then it goes along to the controller, eventually going to the model and then goes full circle. Just like a waiter, it takes requests from the customer (or user) in this context, goes to the chef (or model), gets what the user was requesting (or food) and brings it to the user. Look at the code below for example!
class ProductsController < ApplicationController def index
@products = Product.all
end def show
@product = Product.find(params[:id])
end def create
@product = Product.new(product_params)
end def update
@product = Product.find(params[:id])
@product.update(product_params)
end def destroy
@product = Product.find(params[:id])
@product.delete
end private
def product_params
params.require(:product).permit(:title, :price, :description, :image)
endend
Conclusion
In summation, think of the MVC as an architectural design for web applications in Rails. Each one has a separate responsibility, each one working in conjunction with each other to meet the users’ needs. Please note the code snippets I used are just examples and your code does not have to mirror it! Check out the resource below for more information!