Intro to Ruby on Rails

Resources

  1. Official Ruby on Rails Documentation Ruby on Rails
  2. Ruby on Rails Packages Ruby Gems

Getting Started

  1. Create a new rails application rails new application_name
  2. Run Rails server rails s
  3. Default root route
  • localhost:3000

MVC Overview

It is a separation of the presentation layer and the backend layer

  1. Presentation Layer (what the user sees when interacting with the application)
  2. Backend Layer (All of the logic code for operating the application)

Model Overview

  1. Resources within the application
    • examples: posts, comments, users, listings, etc...
  2. Will generally access a database
    • Sqlite is the default database with Rails

View Overview

  1. Makes up the front-end or presentation layer of the application
  2. Shares data with controllers
    • The app/views directory has a subdirectory for each controller
  3. Operates in standard web templates (HTML, CSS, JavaScript)
    • Ruby is embedded into the HTML file as an extension "action_name.html.erb"
    • Embedded Ruby is done with the HTML tag <%= ... %>

Controller Overview

  1. The backend brains/logic of the application
    • examples: users_controller, listings_controller, pages_controller
  2. Determines how user requests are handled and returned to the user via views

Rails App Structure

App

assets (stores static assets)

config
  • .keep
images
stylesheets (stores css/scss stylesheets)
  • application.css (manifest file that is available to all application views)

channels (makes realtime communication available to the application)

  • channel.rb
  • connection.rb

controllers (where all additional controllers will be stored)

concerns
  • .keep
  • application*controller.rb *(default functionality)_

helpers (helper methods used in views templates)

  • application_helper.rb

javascript (makes JavaScript available to the application)

  • controllers
    • application.js
    • index.js
  • application.js

jobs

  • application_jobs.rb

mailers

  • application_mailers.rb

models (where models are stored)

concerns
  • .keep
  • application_record.rb

views (all views are served from this directory)

layouts
  • application.html.erb

  • mailer.html.erb

  • mailer.text.erb

Bin

  • bundle
  • importmap
  • rails
  • rake
  • setup

Config

environments

  • development.rb
  • production.rb
  • test.rb

initializers

  • assets.rb
  • content_security_policy.rb
  • filter_parameter_logging.rb
  • inflections.rb
  • permissions_policy.rb

locales

  • en.yml

  • application.rb

  • boot.rb

  • cable.yml

  • environment.rb

  • importmap.rb

  • mastery.key

  • puma.rb

  • routes.rb

  • storage.yml

DB

development.sqlite3

seeds.rb

Lib

assets

  • .keep

tasks

  • .keep

Log

.keep

.development.log

Public

404.html

422.html

500.html

Storage

Test

Tmp

Vendor

Basics of MCV Usage

Generate a Controller

Creating New Actions

Creating a new View

Defining the Root Route

Creating Additional Pages (putting it all together)

Databases in Ruby on Rails

Database Layer

Relational Databases

Operations of the Database

  • Create (add rows)
  • Read (display existing rows)
  • Update (modify existing rows)
  • Delete (remove existing rows)

Query Interface

  • SQL (Structured Query Interface)
  • Active Record
    • ORM (Object Relational Mapper)
      • Communicates between Rails Application and Database
      • Translates Ruby Code into SQL queries
      • Location: models > application_record.rb
        • ApplicationRecord inherits functionality from ActiveRecord