Intro to Ruby on Rails: From Zero to Basics

Oppo16 14 views 23 slides Jul 17, 2024
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

Through CodEswatini, we recently hosted an engaging Intro to Web Development: Ruby on Rails workshop, where participants learned the fundamentals of web development using this powerful framework. The session was interactive and packed with hands-on exercises, aimed at equipping attendees with practi...


Slide Content

Intro to Ruby on Rails
22/06/24
Presented by Thuba Mamba

Agenda
•The What, How and Why of Ruby on Rails
•History
•Main Features
•Ruby: from zero to basic
•Ruby on Rails: Associations
•Let’s build an app
•Going Forward
•CI/CL

The What, How, Why of RoR
•RoR is an open-source, web application framework written in Ruby
•RoR was created by David Heinemeier Hansson (aka DHH)in 2004
•Some interesting facts:
•+3.7 million active websites use RoR
•RoR is the 15th most used framework
•Ruby is the 4th most paying technology (Stackoverflow survey)
•RoR is used by some of the biggest tech companies - Basecamp, Hulu, Shopify, Airbnb,
Github, Soundcloud, Coinbase, etc
History

The What, How, Why of RoR
•Model-View-Controller (MVC) Architecture
Main Features
-Separation of “concern/responsibilities” in your code
-Model:
-Structures your data in a reliable form
-Typically reflects real-world things
-View:
-Displays data to the users in easy-to-understand
format (based on the user’s actions)
-Controller:
-Acts as a liaison between the Model and the View
-Receives the user input and decides what to do
with it

The What, How, Why of RoR
•Active Record
Main Features
-A robust and comprehensive toolkit/library that makes it simple for devs to write DB
interaction queries
-Acts as a bridge between the DB and the application, allowing devs to use Ruby code instead of
complex queries
-This helps save developer time, allowing for faster development in that, RoR doesn’t require
multiple configuration files.
•Convention over Configuration
-RoR has numerous testing tools such as RSpec
•Simple Testing Tools

The What, How, Why of RoR
•Scaffolding
Main Features
-A powerful feature in RoR that allows devs to automatically generate basic codes that perform
various operations, without writing everything from scratch.
-Ruby is a simple, concise, flexible object-oriented programming language that close to the
English language
•Simple Programming Language

The What, How, Why of RoR
Benefits
-Speedy Development: Simplifies tasks for faster,
high-quality results.
-Excellent Logic: Handles complex logic accurately.
-Reliable and Accurate: Test-driven, reducing errors
and ensuring consistency.
-Easily Scalable: Easily scales projects without
losing quality.
-Strong Developer Community: Large, skilled developer
community for assistance.

Ruby: from zero to basic
-Ruby files are saved with the extension .rb
-Ruby is case sensitive
-Comments in Ruby are made using the # sign
-For multi-line commends, you can use =begin and =end
-We use puts, print or p to output/display in the console
-Variable names cannot start with numbers, but can contain
numbers at the end
-Signs are not allowed in variable names, except underscores
-Reserved keywords cannot be used as variable names
-Words like “if”, “end”, “class”, “module”, etc.
-String interpolation: You can embed variables inside strings
using “#{}” within double-quoted strings
-Constants: these are variables that are intended to remain
unchanged - there are written in all uppercase letters

Ruby: from zero to basic
Data Types
-Strings: Like a storybook with letters and words
- e.g. “Sawbona”
-Integers: Counting blocks, just whole numbers -
e.g. 10
-Floats: Numbers with parts, like 1.5.
-Booleans: Yes/no buttons.
-Arrays: A box of crayons, each crayon is a
different item - e.g. array = [red, yellow, blue]
-Hashes: Labeled boxes for easy finding - e.g.
hash = { toy: “water_gun”}
-Symbols: Little flags for special thing -
e.g. :my_toy
-Nil: When you have nothing at all.
-Regular Expressions: Magic patterns to find
specific things.
-Ranges: A line of numbers, like 1 to 10.
Operators
-+ Addition
-- Subtraction
-* Multiplication
-/ Division
-** Exponent
-% Modulus

Ruby: from zero to basic
Classes
-A class in OOP serves as a blueprint/template for cleating objects (instances)
-It defines the attributes (data) and behaviours (methods) that the object of the class will
have
-Class Car: Defines a blueprint for creating cars
-Instance Variables (@make, @model): Hold specific data (attributes)
for each car object
-Methods (initialize, display_info): Define behaviors that car objects
can perform, like displaying information about themselves
-Class Employee: Defines a blueprint for creating employee objects with attributes ( name,
age, position).
-Attributes (attr_accessor): Automatically generates getter and setter methods for name,
age, and position.
-Constructor (initialize method): Initializes instance variables ( @name, @age, @position)
when creating new employee objects.

Ruby: from zero to basic
Methods
-A way to encapsulate REUSABLE code into a single unit
that can be called multiple times throughout your program
-They allow you to perform specific tasks, often with
input parameters and returning a result
-Definition: Methods are defined using def and end
-Parameters: Methods can accept parameters for input
-Return Values: Methods return the value of the last
evaluated expression or use return
-Default Parameters: Methods can have default values for
parameters
-Variable Arguments: Methods can accept a variable number
of arguments using *
-Keyword Arguments: Methods can accept named keyword
arguments for clarity

Ruby: from zero to basic
Methods
-Method name conventions:
-Snake Case: Use underscores to separate words
in method names ( calculate_total_amount)
-? and ! Conventions: Use ? for query methods
and ! for dangerous or modifying methods
-Descriptive Names: Choose names that clearly
describe what the method does
-Avoid Ambiguity: Ensure method names are
precise and unambiguous
-Consistency: Maintain consistent naming
conventions throughout your codebase

Ruby: from zero to basic
Strings
-A collection of one or more characters
-It could be made up of letters, numbers or symbols
-Unlike in other languages, Strings are mutable -
this means they can be changed or modified after
being created
-You can create strings using Single (‘’) or double
(“”) quotes
-Commonly used String Methods with examples on the
right

Ruby on Rails: from zero to basic
Associations
-Associations define the relationships between different models in your application's database
-These associations enable ActiveRecord (Rails' Object Relational Mapper [ORM]) to
automatically create SQL queries that retrieve related data efficiently
-Types of associations:
1.One-to-One
2.One-to-Many
3.Many-to-Many
4.Polymorphic

Ruby on Rails: Associations
One-to-One
Each User has exactly one Profile, and each Profile belongs to exactly one
User. This is a one-to-one relationship
One-to-Many
An Author can have many Books, but each Book belongs to only one Author. This
is a one-to-many relationship

Ruby on Rails: Associations
Many-to-Many
Each Doctor can have many Patients through Appointments, and each Patient can
have many Doctors through Appointments. This is a many-to-many relationship
Polymorphic
The Image model can belong to multiple other models ( Article, Product)
through a single association. This is achieved using polymorphism
(imageable_id and imageable_type fields in the images table).

Let’s build an app !

Let’s build an app
Prerequisites
-Ruby on Rails
-To install Rails, you can follow these tutorials:
-https://gorails.com/setup/windows/10
-https://youtu.be/mVvuL_a7fFA
-http://installrails.com/
-Database (Postgres for this session)
-To install Postgres, follow these tutorials:
-https://www.postgresql.org/download/macosx/
-https://youtu.be/0n41UTkOBb0
-https://www.w3schools.com/postgresql/postgresql_install.php
-An IDE (I prefer Jetbrain’s RubyMine but VSCode can also work)

Let’s build an app
After having run all these commands,
start your app by running “rails s”,
“rails server” also works. You should
see a similar page when visiting
http://127.0.0.1:3000/
Please start by running these commands in your
terminal:

Let’s build an app
Project Structure
The structure follows the MVC Architecture
Final code can be found on github.
Final Project

Going Forward
How to expand this app
-These are some of the features and improvements you can add to this app:
-Authentication - Devise ( https://github.com/heartcombo/devise )
-Authorization - cancancan (https://github.com/CanCanCommunity/cancancan), pundit ( https://
github.com/varvet/pundit )
-Search functionality - pg_search ( https://github.com/Casecommons/pg_search ), elastic search
(https://github.com/elastic/elasticsearch-rails )
-Styling - TailwindCSS ( https://tailwindcss.com/ )
-Write tests - RSpec (https://rspec.info/), Minitest (https://github.com/minitest/minitest )

CL/CI
Continued Learning and Continued Improvements
-Here are some resources to continue your learning journey:
-The Rails Way by Obie Fernandez
-Go Rails by Chris Oliver
-Contribute to Open Source - here’s a guide to get started
-https://courses.bigbinaryacademy.com/learn-ruby/
-https://firstrubyfriend.org/
-https://www.reddit.com/r/rails/

Thank you for joining us.