Rails MVC

Thea
2 min readJun 11, 2020

Models: The logic of a web application where data is accessed & manipulated via a database

Views: The frontend, user-facing part of a web application — this is the only part of the app that the user interacts with directly. Consist of HTML, CSS & Javascript

Controllers: The middle man between the client, models & views. The controller relays data from the browser to the application & from the application to the browser

Manually set up a Rails MVC structure

*In practice, don’t include | |, just add the text described inside

*Note: Model names should always be capitalised when referenced inside a file

A. Create Database

1) Create a database table for a model with its attributes in the terminal

*Note: the attributes are SPACE SEPARATED

rails g migration Create|pluralised_model_name| attribute1:datatype … attributeN:datatype

#This will create the table in the db/migrate folder

2) In terminal run the following to complete the migration & update the schema

rails db:migrate 

3) Create a seed file in db/seeds.rb & populate it

4) Once everything is set up, run

rails db:seed

B. Create Models

5) In app/models create the model file

|model_name|.rb

6) In the model file, define the model class to inherit from ActiveRecord::Base

|model_name| < ActiveRecord::Base

7) Declare the associations between models (i.e. has_many, belongs_to)

C. Create Controllers

8) Create the controller file in the app/controller directory

|pluralised_model_name|_controller.rb

9) Inside the controller file create the controller class to inherit from ApplicationController OR ActionController::Base

|pluralised_model_name|Controller < ApplicationController 

OR

|pluralised_model_name|Controller < ActionController::Base

D. Draw Routes

10) In config/routes.rb draw the routes

*Note: The model name & routes are written as symbols

resources :|pluralised_model_name| #Draws all 7 RESTful routes

Or

resources :|pluralised_model_name|, only: [:index, :show] 
#Draws specific defined routes

11) In terminal check what routes are created

rails routes

12) In the controller file, define the controller actions that were stated in config/routes.rb & returned from rails routes

E. Create Views

10) In the app/views directory, create a new folder for the model named

|pluralised_model_name|

11) In said folder, create a .html.erb view file for each controller action that renders a page

controller_action.html.erb #Typically index, new, show & edit

12) Create the necessary forms & tags

*Note: If the name of the tag literally has the word tag in it, it should open with <%=

Convention over configuration

If a folder (posts) & file (index.html.erb) are present in the views folder that correspond to a controller (PostsController) and action (index) listed on a route (posts#index)

Rails will display that view by default — it recognises the match & implicitly renders the file

Rails will display that view by default — it recognises the match & implicitly renders the file

#BlackLivesMatter

--

--