SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
Namespace less engine
Namespace less engineNamespace less engine
Namespace less engine
 
Laravel overview
Laravel overviewLaravel overview
Laravel overview
 
Laravel - The PHP Framework for Web Artisans
Laravel - The PHP Framework for Web ArtisansLaravel - The PHP Framework for Web Artisans
Laravel - The PHP Framework for Web Artisans
 
How angularjs saves rails
How angularjs saves railsHow angularjs saves rails
How angularjs saves rails
 
AngularJS meets Rails
AngularJS meets RailsAngularJS meets Rails
AngularJS meets Rails
 
Laravel Eloquent ORM
Laravel Eloquent ORMLaravel Eloquent ORM
Laravel Eloquent ORM
 
Laravel Introduction
Laravel IntroductionLaravel Introduction
Laravel Introduction
 
Laravel Tutorial PPT
Laravel Tutorial PPTLaravel Tutorial PPT
Laravel Tutorial PPT
 
Laravel tutorial
Laravel tutorialLaravel tutorial
Laravel tutorial
 
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Laravel development (Laravel History, Environment Setup & Laravel Installatio...Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
 
API Development with Laravel
API Development with LaravelAPI Development with Laravel
API Development with Laravel
 
Getting started with laravel
Getting started with laravelGetting started with laravel
Getting started with laravel
 
Rails Engine Patterns
Rails Engine PatternsRails Engine Patterns
Rails Engine Patterns
 
Laravel presentation
Laravel presentationLaravel presentation
Laravel presentation
 
What Is Hobo ?
What Is Hobo ?What Is Hobo ?
What Is Hobo ?
 
Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The Enterprise
 
Projects In Laravel : Learn Laravel Building 10 Projects
Projects In Laravel : Learn Laravel Building 10 ProjectsProjects In Laravel : Learn Laravel Building 10 Projects
Projects In Laravel : Learn Laravel Building 10 Projects
 
Laravel 5.4
Laravel 5.4 Laravel 5.4
Laravel 5.4
 

Ähnlich wie Introduction to Ruby on Rails

Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
sunniboy
 
Rails
RailsRails
Rails
SHC
 

Ähnlich wie Introduction to Ruby on Rails (20)

Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
 
Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web Development
 
Dev streams2
Dev streams2Dev streams2
Dev streams2
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Rails
RailsRails
Rails
 
Ruby Rails Web Development.pdf
Ruby Rails Web Development.pdfRuby Rails Web Development.pdf
Ruby Rails Web Development.pdf
 
Ruby On Rails Siddhesh
Ruby On Rails SiddheshRuby On Rails Siddhesh
Ruby On Rails Siddhesh
 
Ruby on Rails introduction
Ruby on Rails introduction Ruby on Rails introduction
Ruby on Rails introduction
 
TorqueBox
TorqueBoxTorqueBox
TorqueBox
 
Ruby On Rails - Rochester K Linux User Group
Ruby On Rails - Rochester K Linux User GroupRuby On Rails - Rochester K Linux User Group
Ruby On Rails - Rochester K Linux User Group
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
Lecture #5 Introduction to rails
Lecture #5 Introduction to railsLecture #5 Introduction to rails
Lecture #5 Introduction to rails
 
Introduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy HinyukIntroduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy Hinyuk
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 
Ruby on Rails
Ruby on Rails Ruby on Rails
Ruby on Rails
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 
Phoenix for Rails Devs
Phoenix for Rails DevsPhoenix for Rails Devs
Phoenix for Rails Devs
 
Building Application with Ruby On Rails Framework
Building Application with Ruby On Rails FrameworkBuilding Application with Ruby On Rails Framework
Building Application with Ruby On Rails Framework
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 

Kürzlich hochgeladen

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Kürzlich hochgeladen (20)

WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 

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 :) ) ! ale@alessandro.desi
 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