SlideShare a Scribd company logo
Getting started
with
By Basayel Said
Rails
Points to cover ..
❏ What is Rails?
❏ Rails Background
❏ Rails Philosophy
❏ Who uses Rails?
❏ Rails App Structure
❏ MVC Pattern
❏ Rails Components
❏ What is Rake?
❏ What is a Gem?
❏ Useful Links
❏ Demo ( Blog Application )
❏ Assignment
What is Rails?
● Rails is a web application development
framework written in the Ruby
language. It is designed to make
programming web applications easier by
making assumptions about what every
developer needs to get started.
Rails Background
● David Hanson => BaseCamp, Ruby on Rails
● Hanson released Rails as open source in
July 2004.
● December 2005, version 1 was released.
● Became very popular after Apple decided to
bundle and ship it with their Mac OS X,
Leopard, in October 2007.
Rails Philosophy
&
Who uses Rails ?
Rails App Structure
“app”
Application Logic
controllers, views,
models, assets, …
“config”
Database, Routes & more configurations
“db”
Database schema & Migrations
“public”
static files & compiled assets
“vendor”
add-ons to “core” rails
$ rails new Blog
12
MVC Pattern
Getting Started with Rails
Rails Components
● Models
○ Active Record Basics
○ Rails Database Migrations
○ Active Record Validations
○ Active Record Callbacks
○ Active Record Associations
○ Active Record Query
Interface
● Views
○ Layouts and Rendering in
Rails
○ Action View Form Helpers
● Controllers
○ Action Controller overview
○ Rails Routing from the
outside in
Active
Record
What is Active Record?
● Provides an interface and
binding between the tables in a
relational database and of the
Ruby program code that
manipulates database records
● Allows simple designs and
straightforward mappings
between database tables and
application objects
Active Record Migrations
A convenient way to alter your database schema over time
in a consistent and easy way. They use a Ruby DSL so that
you don't have to write SQL by hand, allowing your schema
and changes to be database independent.
Active Record Validations
As you can see, our validation lets us know that our Person
is not valid without a name attribute. The second Person
will not be persisted to the database.
name field must
have value
Active Record Validations ( More .. )
Active Record Callbacks
Callbacks are methods that get called at certain
moments of an object's life cycle. With
callbacks it is possible to write code that will run
whenever an ActiveRecord object is created,
saved, updated, deleted, validated, or loaded
from the database.
ActiveRecord Callbacks ( example )
Available Callbacks
3.1 Creating an
Object
before_validation
after_validation
before_save
around_save
before_create
around_create
after_create
after_save
3.2 Updating
an Object
before_validation
after_validation
before_save
around_save
before_update
after_update
after_save
3.3 Destroying
an Object
before_destroy
around_destroy
after_destroy
MORE ?!, Check
http://guides.rubyonrails.
org/active_record_callbacks.
html
CRUD
article = Article.new
article.published = true
article.save
OR
article = Article.create(published: true)
Article.find(3)
article = Article.find(3)
article.subject = "Rails Tutorial”
article.save
OR
article.update(subject: “Rails Tutorial”)
article = Article.find(3)
article.destroy
Create
Read
Update
Delete
Active Record Associations ( Why? )
Active Record Associations
Rails supports six types of associations:
● belongs_to
● has_one
● has_many
● has_many :through
● has_one :through
● has_and_belongs_to_many
The belongs_to Association
The has_one Association
The has_many Association
The has_many :through Association
The has_and_belongs_to_many Association
The has_one :through Association
Polymorphic Associations
Active Record Query Interface
Active Record Query Interface (Cont.)
MORE ?!, Check http://guides.rubyonrails.org/
active_record_querying.html
Layouts &
Rendering
Layouts and Rendering
Controller automatically render
➢ views with names corresponding to valid
routes.
➢ {action_name}.html.erb
Layouts and Rendering
for HTML, just use the defaults:
get the view that is named the same as
the action, and render it
app/controllers/posts_controller.rb
Layouts and Rendering
app/views/posts/index.html.erb
embedded Ruby (.erb)
automatically provided link helpers
Layouts and Rendering
Layouts
view templateLayout placeholder
Finding Layouts
Rails first looks for a file in app/views/layouts
with the same base name as the controller.
Current Layout
Application Layout
Controller Layout
Inheritance
Action View
Form Helpers
form_tag
● Two arguments
a. path for action : absolutely 1st argument
b. options hash
■ HTTP method :post(default), :get, :put, :delete
■ HTML options :class, :style, ...
form_tag
app/views/home.html.erb current action
Helpers
● text_field_tag
● text_area_tag
● check_box_tag
● radio_button_tag
● password_field_tag
● hidden_field_tag
● search_field_tag
● url_field_tag
● email_field_tag
Generating Form Elements
HTML5 Controls
MORE ?!, Check http://guides.
rubyonrails.org/form_helpers.html
Action
Controller
A Controller
● RESTful applications
● Like an orchestra conductor
● As a middleman between models & views
REST (URI with HTTP methods)
GET /books HTTP/1.1
#index
GET /books/new HTTP/1.1
#new
GET /books/1 HTTP/1.1
#show
POST /books HTTP/1.1
#create
PUT /books/1 HTTP/1.1
#update
GET /books/1/edit HTTP/1.1
#edit
DELETE /books/1 HTTP/1.1 #destroy
Routing
$ rake routes CONTROLLER=books
named routes HTTP verbs resource URI Controllers & Actions
Methods & Actions
● A “class” has methods
● A controller < ApplicationController
● public methods => “action” <= routing
Actions
Rake
Rake a standalone Ruby utility
● Create a file .rake
● Run this rake file with the command rake
my:alarm
What is a Gem?
● Extending functionalities with already packaged
functions.
● i.e user authentication, message system, asset
handlers, geolocation, pagination system, linking to
exterior services such as Amazon AWS, and last but not
least Rails itself) are called Ruby Gems
● Gems are ruby software packages
● Lots of gems can be found in github, but its funner to
search for gems via ruby-gems or ruby-toolbox
Useful Links
➔ Ruby on Rails Guides
➔ Ruby on Rails Screencasts
➔ Rails Best Practices
➔ Rails Style Guide
➔ Testing Rails Applications
➔ Rails Tutorial Ebook by Michael Hartl
Hands On!
Let’s create a Quick Blog Application
Assignment!
Add the ability to comment on any post.
While listing created comments for each
post & a link to remove any comment
Hint:
Use Active Record Associations
Thanks!
@BasayelSaid

More Related Content

What's hot (20)

PPTX
SharePoint and jQuery Essentials
Mark Rackley
 
ODP
Introduction to Swagger
Knoldus Inc.
 
PDF
Caldera Learn - LoopConf WP API + Angular FTW Workshop
CalderaLearn
 
PDF
Introduction à Ruby
Microsoft
 
PPT
Rails review
Alan Hecht
 
PDF
A Practical Guide To Hypermedia APIs - Philly.rb
SmartLogic
 
PDF
MVS: An angular MVC
David Rodenas
 
PDF
Zend Framework Quick Start Walkthrough
Bradley Holt
 
PDF
A Practical Guide to Hypermedia APIs
SmartLogic
 
PDF
Spring rest-doc-2015-11
Eric Ahn
 
PPTX
SPA using Rails & Backbone
Ashan Fernando
 
PDF
WinAppDriver Development
Jeremy Kao
 
PPTX
RESTful API Design Best Practices Using ASP.NET Web API
💻 Spencer Schneidenbach
 
PDF
Introduction to AngularJS For WordPress Developers
Caldera Labs
 
PDF
PyUIA 0.3
Jeremy Kao
 
KEY
Active Model
Derek Hammer
 
PDF
Spring Mvc Rest
Craig Walls
 
PPTX
Apache Cayenne: a Java ORM Alternative
Andrus Adamchik
 
PPTX
Laravel Eloquent ORM
Ba Thanh Huynh
 
PPT
jQuery introduction
Tomi Juhola
 
SharePoint and jQuery Essentials
Mark Rackley
 
Introduction to Swagger
Knoldus Inc.
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
CalderaLearn
 
Introduction à Ruby
Microsoft
 
Rails review
Alan Hecht
 
A Practical Guide To Hypermedia APIs - Philly.rb
SmartLogic
 
MVS: An angular MVC
David Rodenas
 
Zend Framework Quick Start Walkthrough
Bradley Holt
 
A Practical Guide to Hypermedia APIs
SmartLogic
 
Spring rest-doc-2015-11
Eric Ahn
 
SPA using Rails & Backbone
Ashan Fernando
 
WinAppDriver Development
Jeremy Kao
 
RESTful API Design Best Practices Using ASP.NET Web API
💻 Spencer Schneidenbach
 
Introduction to AngularJS For WordPress Developers
Caldera Labs
 
PyUIA 0.3
Jeremy Kao
 
Active Model
Derek Hammer
 
Spring Mvc Rest
Craig Walls
 
Apache Cayenne: a Java ORM Alternative
Andrus Adamchik
 
Laravel Eloquent ORM
Ba Thanh Huynh
 
jQuery introduction
Tomi Juhola
 

Similar to Getting Started with Rails (20)

PPTX
12 Introduction to Rails
Deepak Hagadur Bheemaraju
 
PDF
Lecture #5 Introduction to rails
Evgeniy Hinyuk
 
PDF
Introduction to Rails by Evgeniy Hinyuk
Pivorak MeetUp
 
PPT
Ruby on rails
chamomilla
 
PDF
Ruby On Rails
Balint Erdi
 
DOC
Rails interview questions
Durgesh Tripathi
 
ODP
Ruby On Rails
Mehmet Beydoğan
 
PPT
Ruby On Rails
guest4faf46
 
KEY
Supa fast Ruby + Rails
Jean-Baptiste Feldis
 
PPT
Ruby on Rails: Building Web Applications Is Fun Again!
judofyr
 
PDF
Introduction to Rails - presented by Arman Ortega
arman o
 
PDF
td_mxc_rubyrails_shin
tutorialsruby
 
PDF
td_mxc_rubyrails_shin
tutorialsruby
 
PPT
Ruby On Rails
Gautam Rege
 
PDF
Ruby Rails Web Development
Sonia Simi
 
PPT
Rails
SHC
 
PDF
RubyEnRails2007 - Dr Nic Williams - Keynote
Dr Nic Williams
 
PDF
Aspose pdf
Jim Jones
 
PDF
Introduction to Ruby on Rails
Alessandro DS
 
PDF
مقایسه و بررسی چهارچوب ریلز
railsbootcamp
 
12 Introduction to Rails
Deepak Hagadur Bheemaraju
 
Lecture #5 Introduction to rails
Evgeniy Hinyuk
 
Introduction to Rails by Evgeniy Hinyuk
Pivorak MeetUp
 
Ruby on rails
chamomilla
 
Ruby On Rails
Balint Erdi
 
Rails interview questions
Durgesh Tripathi
 
Ruby On Rails
Mehmet Beydoğan
 
Ruby On Rails
guest4faf46
 
Supa fast Ruby + Rails
Jean-Baptiste Feldis
 
Ruby on Rails: Building Web Applications Is Fun Again!
judofyr
 
Introduction to Rails - presented by Arman Ortega
arman o
 
td_mxc_rubyrails_shin
tutorialsruby
 
td_mxc_rubyrails_shin
tutorialsruby
 
Ruby On Rails
Gautam Rege
 
Ruby Rails Web Development
Sonia Simi
 
Rails
SHC
 
RubyEnRails2007 - Dr Nic Williams - Keynote
Dr Nic Williams
 
Aspose pdf
Jim Jones
 
Introduction to Ruby on Rails
Alessandro DS
 
مقایسه و بررسی چهارچوب ریلز
railsbootcamp
 
Ad

Recently uploaded (20)

PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Top Managed Service Providers in Los Angeles
Captain IT
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
Ad

Getting Started with Rails

  • 2. Points to cover .. ❏ What is Rails? ❏ Rails Background ❏ Rails Philosophy ❏ Who uses Rails? ❏ Rails App Structure ❏ MVC Pattern ❏ Rails Components ❏ What is Rake? ❏ What is a Gem? ❏ Useful Links ❏ Demo ( Blog Application ) ❏ Assignment
  • 3. What is Rails? ● Rails is a web application development framework written in the Ruby language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started.
  • 4. Rails Background ● David Hanson => BaseCamp, Ruby on Rails ● Hanson released Rails as open source in July 2004. ● December 2005, version 1 was released. ● Became very popular after Apple decided to bundle and ship it with their Mac OS X, Leopard, in October 2007.
  • 7. Rails App Structure “app” Application Logic controllers, views, models, assets, … “config” Database, Routes & more configurations “db” Database schema & Migrations “public” static files & compiled assets “vendor” add-ons to “core” rails $ rails new Blog 12
  • 10. Rails Components ● Models ○ Active Record Basics ○ Rails Database Migrations ○ Active Record Validations ○ Active Record Callbacks ○ Active Record Associations ○ Active Record Query Interface ● Views ○ Layouts and Rendering in Rails ○ Action View Form Helpers ● Controllers ○ Action Controller overview ○ Rails Routing from the outside in
  • 12. What is Active Record? ● Provides an interface and binding between the tables in a relational database and of the Ruby program code that manipulates database records ● Allows simple designs and straightforward mappings between database tables and application objects
  • 13. Active Record Migrations A convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent.
  • 14. Active Record Validations As you can see, our validation lets us know that our Person is not valid without a name attribute. The second Person will not be persisted to the database. name field must have value
  • 16. Active Record Callbacks Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an ActiveRecord object is created, saved, updated, deleted, validated, or loaded from the database.
  • 18. Available Callbacks 3.1 Creating an Object before_validation after_validation before_save around_save before_create around_create after_create after_save 3.2 Updating an Object before_validation after_validation before_save around_save before_update after_update after_save 3.3 Destroying an Object before_destroy around_destroy after_destroy MORE ?!, Check http://guides.rubyonrails. org/active_record_callbacks. html
  • 19. CRUD article = Article.new article.published = true article.save OR article = Article.create(published: true) Article.find(3) article = Article.find(3) article.subject = "Rails Tutorial” article.save OR article.update(subject: “Rails Tutorial”) article = Article.find(3) article.destroy Create Read Update Delete
  • 21. Active Record Associations Rails supports six types of associations: ● belongs_to ● has_one ● has_many ● has_many :through ● has_one :through ● has_and_belongs_to_many
  • 25. The has_many :through Association
  • 27. The has_one :through Association
  • 29. Active Record Query Interface
  • 30. Active Record Query Interface (Cont.) MORE ?!, Check http://guides.rubyonrails.org/ active_record_querying.html
  • 32. Layouts and Rendering Controller automatically render ➢ views with names corresponding to valid routes. ➢ {action_name}.html.erb
  • 33. Layouts and Rendering for HTML, just use the defaults: get the view that is named the same as the action, and render it app/controllers/posts_controller.rb
  • 34. Layouts and Rendering app/views/posts/index.html.erb embedded Ruby (.erb) automatically provided link helpers
  • 37. Finding Layouts Rails first looks for a file in app/views/layouts with the same base name as the controller. Current Layout Application Layout Controller Layout Inheritance
  • 39. form_tag ● Two arguments a. path for action : absolutely 1st argument b. options hash ■ HTTP method :post(default), :get, :put, :delete ■ HTML options :class, :style, ...
  • 41. Helpers ● text_field_tag ● text_area_tag ● check_box_tag ● radio_button_tag ● password_field_tag ● hidden_field_tag ● search_field_tag ● url_field_tag ● email_field_tag Generating Form Elements HTML5 Controls MORE ?!, Check http://guides. rubyonrails.org/form_helpers.html
  • 43. A Controller ● RESTful applications ● Like an orchestra conductor ● As a middleman between models & views
  • 44. REST (URI with HTTP methods) GET /books HTTP/1.1 #index GET /books/new HTTP/1.1 #new GET /books/1 HTTP/1.1 #show POST /books HTTP/1.1 #create PUT /books/1 HTTP/1.1 #update GET /books/1/edit HTTP/1.1 #edit DELETE /books/1 HTTP/1.1 #destroy
  • 45. Routing $ rake routes CONTROLLER=books named routes HTTP verbs resource URI Controllers & Actions
  • 46. Methods & Actions ● A “class” has methods ● A controller < ApplicationController ● public methods => “action” <= routing Actions
  • 47. Rake Rake a standalone Ruby utility ● Create a file .rake ● Run this rake file with the command rake my:alarm
  • 48. What is a Gem? ● Extending functionalities with already packaged functions. ● i.e user authentication, message system, asset handlers, geolocation, pagination system, linking to exterior services such as Amazon AWS, and last but not least Rails itself) are called Ruby Gems ● Gems are ruby software packages ● Lots of gems can be found in github, but its funner to search for gems via ruby-gems or ruby-toolbox
  • 49. Useful Links ➔ Ruby on Rails Guides ➔ Ruby on Rails Screencasts ➔ Rails Best Practices ➔ Rails Style Guide ➔ Testing Rails Applications ➔ Rails Tutorial Ebook by Michael Hartl
  • 50. Hands On! Let’s create a Quick Blog Application
  • 51. Assignment! Add the ability to comment on any post. While listing created comments for each post & a link to remove any comment Hint: Use Active Record Associations