SlideShare a Scribd company logo
1 of 52
Download to read offline
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
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

SharePoint and jQuery Essentials
SharePoint and jQuery EssentialsSharePoint and jQuery Essentials
SharePoint and jQuery EssentialsMark Rackley
 
Introduction to Swagger
Introduction to SwaggerIntroduction to Swagger
Introduction to SwaggerKnoldus Inc.
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCaldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCalderaLearn
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à RubyMicrosoft
 
A Practical Guide To Hypermedia APIs - Philly.rb
A Practical Guide To Hypermedia APIs - Philly.rbA Practical Guide To Hypermedia APIs - Philly.rb
A Practical Guide To Hypermedia APIs - Philly.rbSmartLogic
 
Zend Framework Quick Start Walkthrough
Zend Framework Quick Start WalkthroughZend Framework Quick Start Walkthrough
Zend Framework Quick Start WalkthroughBradley Holt
 
A Practical Guide to Hypermedia APIs
A Practical Guide to Hypermedia APIsA Practical Guide to Hypermedia APIs
A Practical Guide to Hypermedia APIsSmartLogic
 
Spring rest-doc-2015-11
Spring rest-doc-2015-11Spring rest-doc-2015-11
Spring rest-doc-2015-11Eric Ahn
 
SPA using Rails & Backbone
SPA using Rails & BackboneSPA using Rails & Backbone
SPA using Rails & BackboneAshan Fernando
 
WinAppDriver Development
WinAppDriver DevelopmentWinAppDriver Development
WinAppDriver DevelopmentJeremy Kao
 
RESTful API Design Best Practices Using ASP.NET Web API
RESTful API Design Best Practices Using ASP.NET Web APIRESTful API Design Best Practices Using ASP.NET Web API
RESTful API Design Best Practices Using ASP.NET Web API💻 Spencer Schneidenbach
 
Introduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersIntroduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersCaldera Labs
 
Apache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM AlternativeApache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM AlternativeAndrus Adamchik
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introductionTomi Juhola
 

What's hot (20)

SharePoint and jQuery Essentials
SharePoint and jQuery EssentialsSharePoint and jQuery Essentials
SharePoint and jQuery Essentials
 
Introduction to Swagger
Introduction to SwaggerIntroduction to Swagger
Introduction to Swagger
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCaldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW Workshop
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
 
Rails review
Rails reviewRails review
Rails review
 
A Practical Guide To Hypermedia APIs - Philly.rb
A Practical Guide To Hypermedia APIs - Philly.rbA Practical Guide To Hypermedia APIs - Philly.rb
A Practical Guide To Hypermedia APIs - Philly.rb
 
MVS: An angular MVC
MVS: An angular MVCMVS: An angular MVC
MVS: An angular MVC
 
Zend Framework Quick Start Walkthrough
Zend Framework Quick Start WalkthroughZend Framework Quick Start Walkthrough
Zend Framework Quick Start Walkthrough
 
A Practical Guide to Hypermedia APIs
A Practical Guide to Hypermedia APIsA Practical Guide to Hypermedia APIs
A Practical Guide to Hypermedia APIs
 
Spring rest-doc-2015-11
Spring rest-doc-2015-11Spring rest-doc-2015-11
Spring rest-doc-2015-11
 
SPA using Rails & Backbone
SPA using Rails & BackboneSPA using Rails & Backbone
SPA using Rails & Backbone
 
WinAppDriver Development
WinAppDriver DevelopmentWinAppDriver Development
WinAppDriver Development
 
RESTful API Design Best Practices Using ASP.NET Web API
RESTful API Design Best Practices Using ASP.NET Web APIRESTful API Design Best Practices Using ASP.NET Web API
RESTful API Design Best Practices Using ASP.NET Web API
 
Introduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersIntroduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress Developers
 
PyUIA 0.3
PyUIA 0.3PyUIA 0.3
PyUIA 0.3
 
Active Model
Active ModelActive Model
Active Model
 
Spring Mvc Rest
Spring Mvc RestSpring Mvc Rest
Spring Mvc Rest
 
Apache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM AlternativeApache Cayenne: a Java ORM Alternative
Apache Cayenne: a Java ORM Alternative
 
Laravel Eloquent ORM
Laravel Eloquent ORMLaravel Eloquent ORM
Laravel Eloquent ORM
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 

Similar to Getting Started with Rails

Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web DevelopmentSonia Simi
 
Rails
RailsRails
RailsSHC
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon RailsPaul Pajo
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Railsanides
 
Jasig rubyon rails
Jasig rubyon railsJasig rubyon rails
Jasig rubyon rails_zaMmer_
 
Jasig rubyon rails
Jasig rubyon railsJasig rubyon rails
Jasig rubyon rails_zaMmer_
 
Introduction To Ruby On Rails
Introduction To Ruby On RailsIntroduction To Ruby On Rails
Introduction To Ruby On RailsSteve Keener
 
Rails Rookies Bootcamp - Blogger
Rails Rookies Bootcamp - BloggerRails Rookies Bootcamp - Blogger
Rails Rookies Bootcamp - BloggerNathanial McConnell
 
Intro to Rails Give Camp Atlanta
Intro to Rails Give Camp AtlantaIntro to Rails Give Camp Atlanta
Intro to Rails Give Camp AtlantaJason Noble
 
Intro to Rails and MVC
Intro to Rails and MVCIntro to Rails and MVC
Intro to Rails and MVCSarah Allen
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorialsunniboy
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2Rory Gianni
 
Ruby On Rails Siddhesh
Ruby On Rails SiddheshRuby On Rails Siddhesh
Ruby On Rails SiddheshSiddhesh Bhobe
 

Similar to Getting Started with Rails (20)

Ruby on rails for beginers
Ruby on rails for beginersRuby on rails for beginers
Ruby on rails for beginers
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web Development
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
 
12 Introduction to Rails
12 Introduction to Rails12 Introduction to Rails
12 Introduction to Rails
 
Rails
RailsRails
Rails
 
Jasig Rubyon Rails
Jasig Rubyon RailsJasig Rubyon Rails
Jasig Rubyon Rails
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Jasig rubyon rails
Jasig rubyon railsJasig rubyon rails
Jasig rubyon rails
 
Jasig rubyon rails
Jasig rubyon railsJasig rubyon rails
Jasig rubyon rails
 
Introduction To Ruby On Rails
Introduction To Ruby On RailsIntroduction To Ruby On Rails
Introduction To Ruby On Rails
 
Rails Rookies Bootcamp - Blogger
Rails Rookies Bootcamp - BloggerRails Rookies Bootcamp - Blogger
Rails Rookies Bootcamp - Blogger
 
Intro to Rails Give Camp Atlanta
Intro to Rails Give Camp AtlantaIntro to Rails Give Camp Atlanta
Intro to Rails Give Camp Atlanta
 
Intro to Rails and MVC
Intro to Rails and MVCIntro to Rails and MVC
Intro to Rails and MVC
 
Laravel overview
Laravel overviewLaravel overview
Laravel overview
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
Ruby On Rails Siddhesh
Ruby On Rails SiddheshRuby On Rails Siddhesh
Ruby On Rails Siddhesh
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Rails interview questions
Rails interview questionsRails interview questions
Rails interview questions
 

Recently uploaded

A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 

Recently uploaded (20)

A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 

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
  • 9.
  • 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