When we are beginner to Ruby on rails , or just getting started , we come across the term MVC . MVC stands for Model , View and Controller . But there are other components involved also . Following image shows how all the components of MVC relate to each other . Now lets have a closer look to each of them .
Following are the core components that we must be familiar with .
- Routing System
Before diving deep into the Rails MVC world , let me give you a bird eye view of whats happening here .
First of all , let us assume that you are the user of this application say AppX !
Now , when you open the site first time , you made a request to browser to load the page . On behalf of you , browser sent a request to appropriate server . Now this server triggers rails routing system and passes URL to it . Now from available information from URL , rails routing system matches it with patterns available to it . If some pattern matches to requested URL , then corresponding controller is triggered by routing system . Btw these patterns are called routes.
Now , controller is triggered . According to functionality required , controller requests data to model , model makes appropriate query to database and returns data to controller . The data loaded in controller is also accessible to respective view file . This view file works as a template which in turn generates web page with all the requested data elements in it . This page is now sent to browser and user can enjoy or work with it !
Now that we have a general idea of whats happening here , we can go a level deeper
Model is one of the core components of MVC architecture.
Model deals with all the data and database related stuff . Like fetching data from DB , saving , updating and deleting records , creating new records . In short , it works with CRUD operations . Not only just CRUD , it is also important when it comes to data validation.
Model is a Ruby Class that is inherited from ActiveRecord::Base . A basic model looks like this
class ApplicationRecord < ActiveRecord::Base self.abstract_class = true end
A very important point to notice is , the only component that interacts with database is Model . Means no other component can directly interact with DB which makes rails applications extremely secextremely secureure when models are implemented properly .
View is second core component of MVC architecture as well as Rails application.
View is nothing but a template file . There are templates like .html.erb , .html.haml etc . But in general , view file contains HTML code along with ruby code .
<% _ruby_code_here_ %> tag is used where we want to execute code but don’t want to show to user .
And <%= _ruby_code_here_ %.> tag is used when we want to execute and show results to user .
Another important thing is that , Views and controllers are closely related .
In any rails application , for each controller , there is a folder in Views folder of application . And for each controller method , there is a view .
From above image we can see that , articles_controller.rb is related to articles directory in views directory . and same is true for comments_controller.rb.
This is third core component of a rails application.
Controllers mostly work as a middle man between database and a view. It is the program where most logic of program is written . Controller triggers other components .
A controller is nothing but a ruby class like model . But unlike model , it is inherited from ApplicationController class .
A controller contains multiple public and private methods . From these methods , only public ones are called Actions . For each Action , there is a Associated View .
Controller contains complete logic of application mainly the CRUD part of it .
A general controller will look like this .
class ArticlesController < ApplicationController def index @article=Article.all end def show @article=Article.find(params[:id]) end def new @article=Article.new end def create @article=Article.new(article_params) if @article.save redirect_to @article else render :new end end def edit @article=Article.find(params[:id]) end def update @article=Article.find(params[:id]) if @article.update(article_params) redirect_to @article else render :edit end end def destroy @article=Article.find(params[:id]) @article.destroy redirect_to root_path end private def article_params params.require(:article).permit(:title,:body) end end
as you can see there are lots of methods . But if you notice their names , these are components of CRUD functionality . Create , Read, Update and Delete .
Routing is not includes in MVC but it is the core component of a rails application . Without routing , we cannot imagine a rails app.
The main task of routing system is to trigger the appropriate controller action based on URL . i.e. if URL is https://some_domain.com/blog/index then routing system will trigger the index action of blog controller .
All the possible routes for a rails application are located in app/config/routes.rb
A typical routes.rb file looks like this .
Database is crucial part of any rails application . We can create rails apps without a database but it will be a simple app.
Any enterprise grade real world application will require a strong database .
Rails support MySql , Sqlite3 and Postgresql by default . We can also use other databases but have to add additional gems .
Rails Database support is so mature that a developer only has to setup it once and we can just work on ruby code without even thinking about it once .
All the database management is done automatically by rails ,by models to be specific .
In ruby , all the components are treated as objects and so do the databases. Every model is associated with a database table . And all the database queries are carried out as it they are functions . i.e. tables are treated as object and all the DB functions are treated as object functions.
If Blog is a model we can just fetch all the data from Blogs table by writting , Blogs.all
Blog --> model blogs -->table in database Blog.all --> calling methods to fetch data from table .
After knowing basics about MVC and general working of a Rails Application , we can say that its a unique and efficient way of designing and developing webapplications . Where users can get what they request , database is separated from other components and there is a easy way to manipulate database too .