SlideShare a Scribd company logo
Alex
Ruby on Rails
for Beginners
Ruby Intro
Types of Objects
String
Integer
Floats
Array
Hash
“london”
12
3.14
[“london”, “paris”]
{first_name: “alex”, last_name: “benoit"}
cities.each do |city|
# …
end
cities = [“london”, “paris”, “new york”]
cities.length # => 3
Class Instances
OOP
class Car
end
car.rb
Car.new
Car.new
Car.new
OOP = Data + Behaviour
MVC
Building a Web Product
Mockup
Let’s mockup the product!
Database Scheme
users
id username email
1 olivier olivier@kudoz.com
2 edward ed@lovelyhood.com
3 vincent vincent@uslide.io
users products
id username email
1 olivier olivier@kudoz.com
2 edward ed@lovelyhood.com
3 vincent vincent@uslide.io
id name url user_id
1 LovelyHood lovely-hood.com
users products
id username email
1 olivier olivier@kudoz.com
2 edward ed@lovelyhood.com
3 vincent vincent@uslide.io
id name url user_id
1 LovelyHood lovely-hood.com 2
users products
id username email
1 olivier olivier@kudoz.com
2 edward ed@lovelyhood.com
3 vincent vincent@uslide.io
id name url user_id
1 LovelyHood lovely-hood.com 2
2 Kudoz getkudoz.com 1
3 uSlide uslide.io 3
4 Freshest frshst.com 2
users products
id username email
1 olivier olivier@kudoz.com
2 edward ed@lovelyhood.com
3 vincent vincent@uslide.io
id name url user_id
1 LovelyHood lovely-hood.com 2
2 Kudoz getkudoz.com 1
3 uSlide uslide.io 3
4 Freshest frshst.com 2
a user has many products
a product belongs to one user
1..N relationship
users products
id username email
1 olivier olivier@kudoz.com
2 edward ed@lovelyhood.com
3 vincent vincent@uslide.io
id name url user_id
1 LovelyHood lovely-hood.com 2
2 Kudoz getkudoz.com 1
3 uSlide uslide.io 3
4 Freshest frshst.com 2
primary key primary key foreign key
Let’s model the database!
Rails Intro
History
Created in 2003 by David Heinemeier Hansson (DHH), while working on Basecamp.
Extracted and released it as open source code in July of 2004
Motto
Convention Configurationover
Ruby community
+100k gems on rubygems.org
Rails new
$ rails new app_name
$ rails new app_name -T --database=postgres
Create the database
$ rails db:create
Launch server
$ rails server
http://localhost:3000
Let’s create the new Rails app!
.
├── app
│ ├──
│ ├──
│ └──
│
└── config
controllers
models
views
└── routes.rb
MVC
config/routes.rb
Router
HTTP
request
MVC
config/routes.rb
Router
app/controllers
Controller
HTTP
request
routed
to controller
MVC
config/routes.rb
Router
app/controllers
Controller
app/models
Model
PostgreSQL

database
HTTP
request
routed
to controller
Get data
from model
MVC
config/routes.rb
Router
app/controllers
Controller
app/models
Model
app/views
View
PostgreSQL

database
HTTP
request
routed
to controller
Get data
from model
Inject it
in view
MVC
config/routes.rb
Router
app/controllers
Controller
app/models
Model
app/views
View
PostgreSQL

database
HTTP
request
routed
to controller
Get data
from model
Inject it
in view
render HTML
MVC
Rails app
config/routes.rb
Router
app/controllers
Controller
app/models
Model
app/views
View
PostgreSQL

database
1 2
3
4
5
Rails Routing
HTTP request?
some link
submit
HTTP basics
A protocol to transfer resources
Based on a system of request / response
Between 2 machines, a client and a server
HTTP request is MORE THAN a URL
HTTP request is 4 things
1 - HTTP verb (GET / POST / PATCH / DELETE)
2 - URL
3 - headers
4 - body (not always)
Rails routing
GET /products
get “home” => “pages#home”
get “about” => “pages#about”
get “products” => “products#index”
post “products” => “products#create”
PagesController
ProductsController
def index
…
end
def create
…
end
PagesController
def home
…
end
def team
…
end
views/pages
home.html.erb
team.html.erb
Action/View convention
ERB
+ =
Embedded Ruby
file.html
<h1>…</h1>
< >
+
file.html.erb
<%= %>
< >
< >
< >
< >
<h1>…</h1>
< >
< >
< >
<%= %>
PagesController
def home
end
pages/home.html.erb
<%= @time %>
<h1>…</h1>
< >
< >
< >
@time = Time.now
Instance variables
Let’s build the pages and product controller!
pages#home pages#about products#index
Active Record
app/models
Model Database
ActiveRecord
$ rails g model Product name:string
class Product < ActiveRecord::Base
…
end
ActiveRecord Model
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.timestamps null: false
end
end
end
ActiveRecord Migration
$ rails db:migrate
Run Pending Migrations
$ rails console
Play with your Model
All
> Product.all
SELECT * FROM products
New & Save
> kudoz = Product.new(name: “kudoz”, url: “getkudoz.com”)
> kudoz.save
INSERT INTO products (name, url)
VALUES ('kudoz', 'getkudoz.com')
> Product.find(1)
SELECT * FROM products
WHERE id = 1
Find
> Product.find_by_name(“kudoz”)
SELECT * FROM products
WHERE name = “kudoz”
Find By
Validations
class Product < ActiveRecord::Base
end
presence: true, uniqueness: truevalidates :name,
Associations
class Product < ActiveRecord::Base
end
belongs_to :user
Let’s build our Product Model!
Rails Params
1 - dynamic URL
GET https://www.facebook.com/:user_name
GET https://www.facebook.com/romainpaillard
params[:user_name] => “romainpaillard”
2 - query string
GET https://www.airbnb.com/s/roma?checkin=02/17/2016
params[:checkin] => 02/17/2016
3 - POST request
POST https://www.lewagon/apply
params[:first_name] => “Boris”
{
“first_name": “Boris",
“last_name”:”Paillard"
} } request 

body
Good news !
params
dynamic URL query string POST request body
Let’s build the show view of a product!
products#show
Rails CRUD Routes
We already built index and show actions!
What’s CRUD?
C
Create
R
Read
U
Update
D
Delete
We always need same actions
As a user, I can CRUD a flat
As a user, I can CRUD a tweet
As a user, I can CRUD a post
As a user, I can CRUD a product
get “products” “products#index”=>
get “products/:id” “products#show”=>
get “products/new” “products#new”=>
post “products” “products#create”=>
get “products/:id/edit” “products#edit”=>
patch “products/:id” “products#update”=>
delete “products/:id” “products#destroy”=>
resources :products
$ rails routes
Let’s rebuild our Routes
but this time using resources!
Rails Helpers for Links
<%= link_to “New Product”, new_product_path %>
<a href=“/products/new”>New Product</a>
Let’s put links in our website
links to home, to product index, to product show
Rails CRUD Routes
Let’s build the new and create actions
get “products/new” “products#new”=>
So that user can create new product…
post “products” “products#create”=>
resources :products, only: [:new, :create]
class ProductsController
def new
@product = Product.new
end
def create
…
end
end
<%= form_for @product do |f| %>
<%= f.text_field :name %>
<%= f.text_field :tagline %>
<%= f.submit %>
<% end %>
<form action="/products" method="post">
<input type="text" name="product[name]" id=“product_name”>
<input type="text" name="product[tagline]" id=“product_tagline">

<input type="hidden" name="authenticity_token" value=“HJYkH..”>

<input type="submit">
</form>
params.require(:product).permit(:name, :tagline)
Strong params (whitelisted)
class ProductsController
def new
@product = Product.new
end
def create
safe_params = params.require(:product).permit(:name, :tagline)
product = Product.new(safe_params)
product.save

redirect_to products_path
end
end
Let’s implement those actions
products#new, products#create
Rails CRUD Routes
Let’s build the edit and update actions
get “products/:id/edit” “products#edit”=>
So that user can edit an existing product…
patch “products/:id” “products#update”=>
resources :products, only: [:edit, :update]
class ProductsController
def edit
@product = Product.find(params[:id])
end
def update
…
end
end
<%= form_for @product do |f| %>
<%= f.text_field :name %>
<%= f.text_field :tagline %>
<%= f.submit %>
<% end %>
<form action="/products" method="post">
<input type="text" name="product[name]" id=“product_name” value=“Product Hunt”>
<input type="text" name="product[tagline]" id=“product_tagline” value=“Great Website!”>

<input type="hidden" name="authenticity_token" value=“HJYkH..”>

<input type="submit">
</form>
params.require(:product).permit(:name, :tagline)
Strong params, again
class ProductsController
def edit
@product = Product.find(params[:id])
end
def create
safe_params = params.require(:product).permit(:name, :tagline)
product = Product.find(params[:id])
product.update(safe_params)

redirect_to products_path
end
end
Let’s implement those actions
products#edit, products#update
Rails CRUD Routes
Let’s build the destroy actions
delete “products/:id” “products#destroy”=>
So that user can delete a product…
resources :products, only: [:destroy]
class ProductsController
def destroy
product = Product.find(params[:id])
product.destroy
redirect_to products_path
end
end
Let’s implement that action
products#destroy
Let’s save this website
Rails for Beginners - Le Wagon
Let’s host this website
Rails for Beginners - Le Wagon
What about my users?
There is a gem for that!

More Related Content

What's hot (20)

PPTX
Java Script
Kalidass Balasubramaniam
 
PDF
Webapps without the web
Remy Sharp
 
PDF
jQuery UI and Plugins
Marc Grabanski
 
PDF
Finding Restfulness - Madrid.rb April 2014
samlown
 
PDF
iPhone Appleless Apps
Remy Sharp
 
PDF
Merb jQuery
Yehuda Katz
 
PDF
Refresh Austin - Intro to Dexy
ananelson
 
PDF
Velocity EU 2014 — Offline-first web apps
andrewsmatt
 
PDF
jQuery Presentation to Rails Developers
Yehuda Katz
 
PPTX
Web Automation Testing Using Selenium
Pete Chen
 
PDF
Rails 4.0
Robert Gogolok
 
PDF
Crafting Quality PHP Applications (Bucharest Tech Week 2017)
James Titcumb
 
PDF
Future of Web Apps: Google Gears
dion
 
PDF
Introduction to plugin development
Caldera Labs
 
PPTX
Designing and developing mobile web applications with Mockup, Sencha Touch an...
Matteo Collina
 
PDF
RSpec: Feature specs as checklist
Edward Fox
 
PDF
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
Ryan Weaver
 
PDF
OpenERP and Perl
OpusVL
 
PDF
Crafting Quality PHP Applications (PHPkonf 2018)
James Titcumb
 
PDF
Crafting Quality PHP Applications (PHP Benelux 2018)
James Titcumb
 
Webapps without the web
Remy Sharp
 
jQuery UI and Plugins
Marc Grabanski
 
Finding Restfulness - Madrid.rb April 2014
samlown
 
iPhone Appleless Apps
Remy Sharp
 
Merb jQuery
Yehuda Katz
 
Refresh Austin - Intro to Dexy
ananelson
 
Velocity EU 2014 — Offline-first web apps
andrewsmatt
 
jQuery Presentation to Rails Developers
Yehuda Katz
 
Web Automation Testing Using Selenium
Pete Chen
 
Rails 4.0
Robert Gogolok
 
Crafting Quality PHP Applications (Bucharest Tech Week 2017)
James Titcumb
 
Future of Web Apps: Google Gears
dion
 
Introduction to plugin development
Caldera Labs
 
Designing and developing mobile web applications with Mockup, Sencha Touch an...
Matteo Collina
 
RSpec: Feature specs as checklist
Edward Fox
 
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
Ryan Weaver
 
OpenERP and Perl
OpusVL
 
Crafting Quality PHP Applications (PHPkonf 2018)
James Titcumb
 
Crafting Quality PHP Applications (PHP Benelux 2018)
James Titcumb
 

Similar to Rails for Beginners - Le Wagon (20)

PDF
Introduction à Ruby
Microsoft
 
KEY
Supa fast Ruby + Rails
Jean-Baptiste Feldis
 
PDF
Ruby on Rails - Introduction
Vagmi Mudumbai
 
PPTX
Ruby on Rails + AngularJS + Twitter Bootstrap
Marcio Marinho
 
PDF
Pourquoi ruby et rails déchirent
Nicolas Ledez
 
PDF
The Rails Way
Michał Orman
 
KEY
Ruby/Rails
rstankov
 
PDF
Rails antipattern-public
Chul Ju Hong
 
PDF
Rails antipatterns
Chul Ju Hong
 
PDF
Pourquoi Ruby on Rails ça déchire ?
Simon Courtois
 
PDF
Rails 3: Dashing to the Finish
Yehuda Katz
 
PDF
Android L01 - Warm Up
Mohammad Shaker
 
PDF
Rails 3 overview
Yehuda Katz
 
PDF
Phoenix for Rails Devs
Diacode
 
PDF
Intro to-rails-webperf
New Relic
 
PDF
Why ruby on rails
Boris Dinkevich
 
PDF
Resource and view
Papp Laszlo
 
PDF
From Backbone to Ember and Back(bone) Again
jonknapp
 
PDF
Crossing the Bridge: Connecting Rails and your Front-end Framework
Daniel Spector
 
PDF
GDG Almaty Meetup: Reactive full-stack .NET web applications with WebSharper
granicz
 
Introduction à Ruby
Microsoft
 
Supa fast Ruby + Rails
Jean-Baptiste Feldis
 
Ruby on Rails - Introduction
Vagmi Mudumbai
 
Ruby on Rails + AngularJS + Twitter Bootstrap
Marcio Marinho
 
Pourquoi ruby et rails déchirent
Nicolas Ledez
 
The Rails Way
Michał Orman
 
Ruby/Rails
rstankov
 
Rails antipattern-public
Chul Ju Hong
 
Rails antipatterns
Chul Ju Hong
 
Pourquoi Ruby on Rails ça déchire ?
Simon Courtois
 
Rails 3: Dashing to the Finish
Yehuda Katz
 
Android L01 - Warm Up
Mohammad Shaker
 
Rails 3 overview
Yehuda Katz
 
Phoenix for Rails Devs
Diacode
 
Intro to-rails-webperf
New Relic
 
Why ruby on rails
Boris Dinkevich
 
Resource and view
Papp Laszlo
 
From Backbone to Ember and Back(bone) Again
jonknapp
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Daniel Spector
 
GDG Almaty Meetup: Reactive full-stack .NET web applications with WebSharper
granicz
 
Ad

Recently uploaded (20)

PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Ad

Rails for Beginners - Le Wagon