SlideShare a Scribd company logo
Agile Web Development
Introduction to 

Ruby on Rails
About these slides
I prepared this presentation to introduce Ruby on Rails to
a group of students at Università di Catania.
It is not enough to get a good grasp of Rails, the
presentation in fact was supported by live coding, where
I started created a Phrasalbook (no more blog engine
please :) )
!
ale@alessandro.desi

http://alessandro.desi
What is Ruby on Rails ?
• Ruby is a programming language. It was created 20 years ago by Yukihiro
“Matz” Matsumoto.
• Rails is a framework for building websites. David Heinemeier Hansson is its
creator (DHH). He gave it the name “Ruby on Rails"
What is Ruby on Rails ?
Rails combines the Ruby programming language with HTML,
CSS, and JavaScript to create a web application that runs on a
web server.
Because it runs on a web server, Rails is considered a server-
side, or “back end,” web application development platform
Why Rails ?
The virtue of Rails is that DHH and the core team that joined him, decided that
there is one best way to implement much of the infrastructure required by a web
application.
Rails Guiding
Principles
• there is a "Rails way": If you follow the Rails conventions, you’ll have fewer
decisions to make and you’ll find more of what you need is already built.
• Convention over Configuration
• Don't Repeat Yourself
Do You Need to Study
Ruby to Learn Rails?
YES ... but you can start with little Ruby knowledge
Rails MVC pattern
• it is visible in the file structure of a Rails application
!
• it exists in the form of a hierarchy of Rails classes, notably ActionController
(controller), ActionView (view), and ActiveRecord (model)
http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/
Let's start:
rails new app_name
that little command will create a huge amount of stuff!
ta-da!
(structure of a Rails application)
Gemfile
• Gemfile it is used by Bundler
• Bundler provides a way for Ruby projects to instal the exact
gems and versions that are needed
• To install the gems you need, just run: bundle install
Other rails commands
rails COMMAND [ARGS]
• generate: Generate new code
• console: Start the Rails console
• server: Start the Rails server
• dbconsole: Start a console for the database specified
in config/database.yml
• new: Create a new Rails application
!
(short-cut alias: g, c, s, db, new)
rails server
Let's play with MVC
We are going to:
• add a route
• create the controller and action associated to the route
• create a model to get some data
!
We need something to play with ... let's create a Phrasal
book
Routing
• The Rails router recognizes URLs and dispatches them to a controller's action.
• The routes are in /confing/routes.rb!
• There are two kind of routing: resourceful and non-resourceful
non-resourceful
for example:
get 'phrases' => 'phrases#index'!
With this route, Rails will match an incoming path of
"/phrases" to the index action of PhrasesController
Controller
• controller is responsible for making sense of the request and
producing the appropriate output
• It makes the model data available to the view so it can display
that data to the user, and it saves or updates data from the
user to the model.
Controller example
class PhrasesController < ApplicationController !
def index!
...!
end !
end!
!
Notice naming convention of controllers in Rails favors pluralization of the last word in the controller's
name, although it is not strictly required.
Action View
For each controller there is an associated directory in the
app/views directory which holds the template files that
make up the views associated with that controller
controller: "Phrases" ---> views: "app/views/phrases/" directory
action: "index" ---> view: "app/views/phrases/index.html.erb"
!
ERB Templates
Action View templates can be written in several ways.
If the template file has a .erb extension then it uses a
mixture of ERB (Embedded Ruby) and HTML
<h1>List of the Phrases</h1>

<% @phrases.each do |phrase| %>

<p><%= phrase %></p>

<% end %>
Model - ActiveRecord
• It is the layer of the system responsible for representing business data and
logic
• In ActiveRecord, objects carry both persistent data and behavior which
operates on that data
• ActiveRecord is the ORM (Object Relational Mapping) inside Rails
How to create a model ?
Do you remember the rails generator ?
Let's use it:
!
rails g model phrases body:string lang_code:string!
!
It creates many files, the most important are:
• the migration: db/migrate/20141216180923_create_phrases.rb
• the model class: app/models/phrases.rb
the migration
It contains the instructions to "migrate" the database:!
class CreatePhrases < ActiveRecord::Migration!
def change!
create_table :phrases do |t|!
t.string :body!
t.string :code_lang!
t.timestamps!
end !
end!
end!
!
in the console issue:
bundle exec rake db:migrate!
to run the migration
CRUD
Reading and Writing Data
Create!
Phrases.create body: "hello, how are you?"!
Phrases.new; .save!
Read!
Phrase.first; .last; .all; .where!
Update!
phrase.body = "..."; .save!
Destroy!
phrase.destroy
RESTful route
Adding in the route.rb: resource :phrases you get the
following routes
GET /phrases ---> phrases#index

POST /phrases ---> phrases#create new_phrase

GET /phrases/new ---> phrases#newedit_phrase

GET /phrases/:id/edit ---> phrases#edit

GET /phrases/:id ---> phrases#show

PATCH /phrases/:id ---> phrases#update

PUT /phrases/:id ---> phrases#update

DELETE /phrases/:id ---> phrases#destroy
tip: "bundle exec rake routes" to check the routes
I want it all - I want it now!
try the scaffold generator :)
Useful if you want to get a plain RESTful resource with
• routes
• model representing the resource
• controller with CRUD actions
• CRUD views
• migration
• tests
... and fully according to rails conventions
just with: "rails generate scaffold ... "
Grazie!
Let's stay in touch!
Esiste il Catania Ruby User Group, potete iscrivervi e
postare tutti i problemi che incontrerete.
Il modo migliore per far pratica e' realizzate un vostro
progetto e lavorare sulle idee che vi appassionano.
!
Alessandro De Simone

http://alessandro.desi

More Related Content

What's hot (20)

ODP
Introduction to Ruby on Rails
hasan2000
 
PDF
Ruby on Rails
DelphiCon
 
KEY
Namespace less engine
shaokun
 
PPTX
Laravel overview
Obinna Akunne
 
PDF
Laravel - The PHP Framework for Web Artisans
Windzoon Technologies
 
PDF
How angularjs saves rails
Michael He
 
PDF
AngularJS meets Rails
Elena Torró
 
PPTX
Laravel Eloquent ORM
Ba Thanh Huynh
 
PDF
Laravel Introduction
Ahmad Shah Hafizan Hamidin
 
PPTX
Laravel Tutorial PPT
Piyush Aggarwal
 
PDF
Laravel tutorial
Broker IG
 
PPTX
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Dilouar Hossain
 
PPTX
API Development with Laravel
Michael Peacock
 
PPTX
Getting started with laravel
Advance Idea Infotech
 
PPTX
Rails Engine Patterns
Andy Maleh
 
PDF
Laravel presentation
Toufiq Mahmud
 
PPT
What Is Hobo ?
Evarist Lobo
 
PDF
Merb For The Enterprise
Matt Aimonetti
 
ODP
Projects In Laravel : Learn Laravel Building 10 Projects
Sam Dias
 
PDF
Laravel 5.4
Nisha Patel
 
Introduction to Ruby on Rails
hasan2000
 
Ruby on Rails
DelphiCon
 
Namespace less engine
shaokun
 
Laravel overview
Obinna Akunne
 
Laravel - The PHP Framework for Web Artisans
Windzoon Technologies
 
How angularjs saves rails
Michael He
 
AngularJS meets Rails
Elena Torró
 
Laravel Eloquent ORM
Ba Thanh Huynh
 
Laravel Introduction
Ahmad Shah Hafizan Hamidin
 
Laravel Tutorial PPT
Piyush Aggarwal
 
Laravel tutorial
Broker IG
 
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Dilouar Hossain
 
API Development with Laravel
Michael Peacock
 
Getting started with laravel
Advance Idea Infotech
 
Rails Engine Patterns
Andy Maleh
 
Laravel presentation
Toufiq Mahmud
 
What Is Hobo ?
Evarist Lobo
 
Merb For The Enterprise
Matt Aimonetti
 
Projects In Laravel : Learn Laravel Building 10 Projects
Sam Dias
 
Laravel 5.4
Nisha Patel
 

Similar to Introduction to Ruby on Rails (20)

PDF
Ruby Rails Web Development
Sonia Simi
 
PDF
Lecture #5 Introduction to rails
Evgeniy Hinyuk
 
PDF
Introduction to Rails by Evgeniy Hinyuk
Pivorak MeetUp
 
KEY
Supa fast Ruby + Rails
Jean-Baptiste Feldis
 
PDF
Ruby On Rails
Balint Erdi
 
KEY
Wed Development on Rails
James Gray
 
PDF
Introduction to rails
Go Asgard
 
PPT
Ruby on rails
chamomilla
 
PDF
Ruby on rails RAD
Alina Danila
 
PDF
Ruby On Rails
anides
 
PPT
Ruby On Rails Seminar Basis Softexpo Feb2010
arif44
 
PDF
Introduction to Rails - presented by Arman Ortega
arman o
 
PPT
Viridians on Rails
Viridians
 
KEY
Intro to Ruby on Rails
rschmukler
 
PPT
Introduction to Ruby on Rails
Manoj Kumar
 
PPT
A Tour of Ruby On Rails
David Keener
 
PPTX
Rubyonrails 120409061835-phpapp02
sagaroceanic11
 
PDF
RubyEnRails2007 - Dr Nic Williams - Keynote
Dr Nic Williams
 
PPT
Ruby on Rails introduction
Tran Hung
 
PPT
Ruby On Rails
guest4faf46
 
Ruby Rails Web Development
Sonia Simi
 
Lecture #5 Introduction to rails
Evgeniy Hinyuk
 
Introduction to Rails by Evgeniy Hinyuk
Pivorak MeetUp
 
Supa fast Ruby + Rails
Jean-Baptiste Feldis
 
Ruby On Rails
Balint Erdi
 
Wed Development on Rails
James Gray
 
Introduction to rails
Go Asgard
 
Ruby on rails
chamomilla
 
Ruby on rails RAD
Alina Danila
 
Ruby On Rails
anides
 
Ruby On Rails Seminar Basis Softexpo Feb2010
arif44
 
Introduction to Rails - presented by Arman Ortega
arman o
 
Viridians on Rails
Viridians
 
Intro to Ruby on Rails
rschmukler
 
Introduction to Ruby on Rails
Manoj Kumar
 
A Tour of Ruby On Rails
David Keener
 
Rubyonrails 120409061835-phpapp02
sagaroceanic11
 
RubyEnRails2007 - Dr Nic Williams - Keynote
Dr Nic Williams
 
Ruby on Rails introduction
Tran Hung
 
Ruby On Rails
guest4faf46
 
Ad

Recently uploaded (20)

PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPT
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Tally software_Introduction_Presentation
AditiBansal54083
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Ad

Introduction to Ruby on Rails

  • 1. Agile Web Development Introduction to 
 Ruby on Rails
  • 2. About these slides I prepared this presentation to introduce Ruby on Rails to a group of students at Università di Catania. It is not enough to get a good grasp of Rails, the presentation in fact was supported by live coding, where I started created a Phrasalbook (no more blog engine please :) ) ! [email protected]
 http://alessandro.desi
  • 3. What is Ruby on Rails ? • Ruby is a programming language. It was created 20 years ago by Yukihiro “Matz” Matsumoto. • Rails is a framework for building websites. David Heinemeier Hansson is its creator (DHH). He gave it the name “Ruby on Rails"
  • 4. What is Ruby on Rails ? Rails combines the Ruby programming language with HTML, CSS, and JavaScript to create a web application that runs on a web server. Because it runs on a web server, Rails is considered a server- side, or “back end,” web application development platform
  • 5. Why Rails ? The virtue of Rails is that DHH and the core team that joined him, decided that there is one best way to implement much of the infrastructure required by a web application.
  • 6. Rails Guiding Principles • there is a "Rails way": If you follow the Rails conventions, you’ll have fewer decisions to make and you’ll find more of what you need is already built. • Convention over Configuration • Don't Repeat Yourself
  • 7. Do You Need to Study Ruby to Learn Rails? YES ... but you can start with little Ruby knowledge
  • 8. Rails MVC pattern • it is visible in the file structure of a Rails application ! • it exists in the form of a hierarchy of Rails classes, notably ActionController (controller), ActionView (view), and ActiveRecord (model)
  • 10. Let's start: rails new app_name that little command will create a huge amount of stuff!
  • 11. ta-da! (structure of a Rails application)
  • 12. Gemfile • Gemfile it is used by Bundler • Bundler provides a way for Ruby projects to instal the exact gems and versions that are needed • To install the gems you need, just run: bundle install
  • 13. Other rails commands rails COMMAND [ARGS] • generate: Generate new code • console: Start the Rails console • server: Start the Rails server • dbconsole: Start a console for the database specified in config/database.yml • new: Create a new Rails application ! (short-cut alias: g, c, s, db, new)
  • 15. Let's play with MVC We are going to: • add a route • create the controller and action associated to the route • create a model to get some data ! We need something to play with ... let's create a Phrasal book
  • 16. Routing • The Rails router recognizes URLs and dispatches them to a controller's action. • The routes are in /confing/routes.rb! • There are two kind of routing: resourceful and non-resourceful
  • 17. non-resourceful for example: get 'phrases' => 'phrases#index'! With this route, Rails will match an incoming path of "/phrases" to the index action of PhrasesController
  • 18. Controller • controller is responsible for making sense of the request and producing the appropriate output • It makes the model data available to the view so it can display that data to the user, and it saves or updates data from the user to the model.
  • 19. Controller example class PhrasesController < ApplicationController ! def index! ...! end ! end! ! Notice naming convention of controllers in Rails favors pluralization of the last word in the controller's name, although it is not strictly required.
  • 20. Action View For each controller there is an associated directory in the app/views directory which holds the template files that make up the views associated with that controller controller: "Phrases" ---> views: "app/views/phrases/" directory action: "index" ---> view: "app/views/phrases/index.html.erb" !
  • 21. ERB Templates Action View templates can be written in several ways. If the template file has a .erb extension then it uses a mixture of ERB (Embedded Ruby) and HTML <h1>List of the Phrases</h1>
 <% @phrases.each do |phrase| %>
 <p><%= phrase %></p>
 <% end %>
  • 22. Model - ActiveRecord • It is the layer of the system responsible for representing business data and logic • In ActiveRecord, objects carry both persistent data and behavior which operates on that data • ActiveRecord is the ORM (Object Relational Mapping) inside Rails
  • 23. How to create a model ? Do you remember the rails generator ? Let's use it: ! rails g model phrases body:string lang_code:string! ! It creates many files, the most important are: • the migration: db/migrate/20141216180923_create_phrases.rb • the model class: app/models/phrases.rb
  • 24. the migration It contains the instructions to "migrate" the database:! class CreatePhrases < ActiveRecord::Migration! def change! create_table :phrases do |t|! t.string :body! t.string :code_lang! t.timestamps! end ! end! end! ! in the console issue: bundle exec rake db:migrate! to run the migration
  • 25. CRUD Reading and Writing Data Create! Phrases.create body: "hello, how are you?"! Phrases.new; .save! Read! Phrase.first; .last; .all; .where! Update! phrase.body = "..."; .save! Destroy! phrase.destroy
  • 26. RESTful route Adding in the route.rb: resource :phrases you get the following routes GET /phrases ---> phrases#index
 POST /phrases ---> phrases#create new_phrase
 GET /phrases/new ---> phrases#newedit_phrase
 GET /phrases/:id/edit ---> phrases#edit
 GET /phrases/:id ---> phrases#show
 PATCH /phrases/:id ---> phrases#update
 PUT /phrases/:id ---> phrases#update
 DELETE /phrases/:id ---> phrases#destroy tip: "bundle exec rake routes" to check the routes
  • 27. I want it all - I want it now! try the scaffold generator :) Useful if you want to get a plain RESTful resource with • routes • model representing the resource • controller with CRUD actions • CRUD views • migration • tests ... and fully according to rails conventions just with: "rails generate scaffold ... "
  • 28. Grazie! Let's stay in touch! Esiste il Catania Ruby User Group, potete iscrivervi e postare tutti i problemi che incontrerete. Il modo migliore per far pratica e' realizzate un vostro progetto e lavorare sulle idee che vi appassionano. ! Alessandro De Simone
 http://alessandro.desi