際際滷

際際滷Share 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, youll have fewer
decisions to make and youll 鍖nd more of what you need is already built.
 Convention over Con鍖guration
 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 鍖le 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)
Gem鍖le
 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 speci鍖ed
in con鍖g/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 /con鍖ng/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 鍖les 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 鍖le 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 鍖les, 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)

Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
hasan2000
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
DelphiCon
Namespace less engine
Namespace less engineNamespace less engine
Namespace less engine
shaokun
Laravel overview
Laravel overviewLaravel overview
Laravel overview
Obinna Akunne
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
Windzoon Technologies
How angularjs saves rails
How angularjs saves railsHow angularjs saves rails
How angularjs saves rails
Michael He
AngularJS meets Rails
AngularJS meets RailsAngularJS meets Rails
AngularJS meets Rails
Elena Torr坦
Laravel Eloquent ORM
Laravel Eloquent ORMLaravel Eloquent ORM
Laravel Eloquent ORM
Ba Thanh Huynh
Laravel Introduction
Laravel IntroductionLaravel Introduction
Laravel Introduction
Ahmad Shah Hafizan Hamidin
Laravel Tutorial PPT
Laravel Tutorial PPTLaravel Tutorial PPT
Laravel Tutorial PPT
Piyush Aggarwal
Laravel tutorial
Laravel tutorialLaravel tutorial
Laravel tutorial
Broker IG
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...
Dilouar Hossain
API Development with Laravel
API Development with LaravelAPI Development with Laravel
API Development with Laravel
Michael Peacock
Getting started with laravel
Getting started with laravelGetting started with laravel
Getting started with laravel
Advance Idea Infotech
Rails Engine Patterns
Rails Engine PatternsRails Engine Patterns
Rails Engine Patterns
Andy Maleh
Laravel presentation
Laravel presentationLaravel presentation
Laravel presentation
Toufiq Mahmud
What Is Hobo ?
What Is Hobo ?What Is Hobo ?
What Is Hobo ?
Evarist Lobo
Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The Enterprise
Matt Aimonetti
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
Sam Dias
Laravel 5.4
Laravel 5.4 Laravel 5.4
Laravel 5.4
Nisha Patel
Introduction to Ruby on Rails
Introduction to Ruby on RailsIntroduction to Ruby on Rails
Introduction to Ruby on Rails
hasan2000
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
DelphiCon
Namespace less engine
Namespace less engineNamespace less engine
Namespace less engine
shaokun
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
Windzoon Technologies
How angularjs saves rails
How angularjs saves railsHow angularjs saves rails
How angularjs saves rails
Michael He
AngularJS meets Rails
AngularJS meets RailsAngularJS meets Rails
AngularJS meets Rails
Elena Torr坦
Laravel Eloquent ORM
Laravel Eloquent ORMLaravel Eloquent ORM
Laravel Eloquent ORM
Ba Thanh Huynh
Laravel tutorial
Laravel tutorialLaravel tutorial
Laravel tutorial
Broker IG
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...
Dilouar Hossain
API Development with Laravel
API Development with LaravelAPI Development with Laravel
API Development with Laravel
Michael Peacock
Rails Engine Patterns
Rails Engine PatternsRails Engine Patterns
Rails Engine Patterns
Andy Maleh
Laravel presentation
Laravel presentationLaravel presentation
Laravel presentation
Toufiq Mahmud
Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The Enterprise
Matt Aimonetti
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
Sam Dias

Similar to Introduction to Ruby on Rails (20)

Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
sunniboy
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
Jean-Baptiste Feldis
Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web Development
Sonia Simi
Dev streams2
Dev streams2Dev streams2
Dev streams2
David Mc Donagh
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
anides
Rails
RailsRails
Rails
SHC
Ruby Rails Web Development.pdf
Ruby Rails Web Development.pdfRuby Rails Web Development.pdf
Ruby Rails Web Development.pdf
SEO expate Bangladesh Ltd
Ruby On Rails Siddhesh
Ruby On Rails SiddheshRuby On Rails Siddhesh
Ruby On Rails Siddhesh
Siddhesh Bhobe
Ruby on Rails Scaffold_ Create Your App In Minutes
Ruby on Rails Scaffold_ Create Your App In MinutesRuby on Rails Scaffold_ Create Your App In Minutes
Ruby on Rails Scaffold_ Create Your App In Minutes
rorbitssoftware
Ruby on Rails introduction
Ruby on Rails introduction Ruby on Rails introduction
Ruby on Rails introduction
Tran Hung
TorqueBox
TorqueBoxTorqueBox
TorqueBox
bobmcwhirter
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
Jose de Leon
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
Rory Gianni
Introduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy HinyukIntroduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy Hinyuk
Pivorak MeetUp
Lecture #5 Introduction to rails
Lecture #5 Introduction to railsLecture #5 Introduction to rails
Lecture #5 Introduction to rails
Evgeniy Hinyuk
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
TAInteractive
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
TAInteractive
Ruby on Rails
Ruby on Rails Ruby on Rails
Ruby on Rails
thinkahead.net
Phoenix for Rails Devs
Phoenix for Rails DevsPhoenix for Rails Devs
Phoenix for Rails Devs
Diacode
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
Edureka!
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
sunniboy
Ruby Rails Web Development
Ruby Rails Web DevelopmentRuby Rails Web Development
Ruby Rails Web Development
Sonia Simi
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
anides
Rails
RailsRails
Rails
SHC
Ruby On Rails Siddhesh
Ruby On Rails SiddheshRuby On Rails Siddhesh
Ruby On Rails Siddhesh
Siddhesh Bhobe
Ruby on Rails Scaffold_ Create Your App In Minutes
Ruby on Rails Scaffold_ Create Your App In MinutesRuby on Rails Scaffold_ Create Your App In Minutes
Ruby on Rails Scaffold_ Create Your App In Minutes
rorbitssoftware
Ruby on Rails introduction
Ruby on Rails introduction Ruby on Rails introduction
Ruby on Rails introduction
Tran Hung
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
Jose de Leon
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
Rory Gianni
Introduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy HinyukIntroduction to Rails by Evgeniy Hinyuk
Introduction to Rails by Evgeniy Hinyuk
Pivorak MeetUp
Lecture #5 Introduction to rails
Lecture #5 Introduction to railsLecture #5 Introduction to rails
Lecture #5 Introduction to rails
Evgeniy Hinyuk
Phoenix for Rails Devs
Phoenix for Rails DevsPhoenix for Rails Devs
Phoenix for Rails Devs
Diacode
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
Edureka!

Recently uploaded (20)

Byteexpo Call Center - Presentation.pptx
Byteexpo Call Center - Presentation.pptxByteexpo Call Center - Presentation.pptx
Byteexpo Call Center - Presentation.pptx
hmk11790
wAIred_VoxxedDaysAmsterdam_03042025.pptx
wAIred_VoxxedDaysAmsterdam_03042025.pptxwAIred_VoxxedDaysAmsterdam_03042025.pptx
wAIred_VoxxedDaysAmsterdam_03042025.pptx
SimonedeGijt
Adobe XD Crack Version 2025 Free Download
Adobe XD Crack Version 2025 Free DownloadAdobe XD Crack Version 2025 Free Download
Adobe XD Crack Version 2025 Free Download
mohsinrazakpa67
microsoft office 2019 crack free download
microsoft office 2019 crack free downloadmicrosoft office 2019 crack free download
microsoft office 2019 crack free download
mohsinrazakpa39
Choreo - The AI-Native Internal Developer Platform as a Service: Overview
Choreo - The AI-Native Internal Developer Platform as a Service: OverviewChoreo - The AI-Native Internal Developer Platform as a Service: Overview
Choreo - The AI-Native Internal Developer Platform as a Service: Overview
WSO2
Driver Genius 24 Crack 2025 License Key Free Download
Driver Genius 24 Crack 2025 License Key Free DownloadDriver Genius 24 Crack 2025 License Key Free Download
Driver Genius 24 Crack 2025 License Key Free Download
umeerbinfaizan
Oracle Database administration Security PPT
Oracle Database administration Security PPTOracle Database administration Security PPT
Oracle Database administration Security PPT
pshankarnarayan
Rights, Copyrights, and Licences for Software Engineering Research v1.0
Rights, Copyrights, and Licences for Software Engineering Research v1.0Rights, Copyrights, and Licences for Software Engineering Research v1.0
Rights, Copyrights, and Licences for Software Engineering Research v1.0
Yann-Ga谷l Gu辿h辿neuc
The Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
The Open-Closed Principle - Part 2 - The Contemporary Version - An IntroductionThe Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
The Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
Philip Schwarz
Wondershare Filmora 14.3.2.11147 crack
Wondershare Filmora   14.3.2.11147 crackWondershare Filmora   14.3.2.11147 crack
Wondershare Filmora 14.3.2.11147 crack
blouch51kp
Clip Studio Paint EX Download (Latest 2025)
Clip Studio Paint EX Download (Latest 2025)Clip Studio Paint EX Download (Latest 2025)
Clip Studio Paint EX Download (Latest 2025)
mohsinrazakpa79
The Open-Closed Principle - Part 1 - The Original Version
The Open-Closed Principle - Part 1 - The Original VersionThe Open-Closed Principle - Part 1 - The Original Version
The Open-Closed Principle - Part 1 - The Original Version
Philip Schwarz
The Evolution of Microsoft Project Portfolio Management
The Evolution of Microsoft Project Portfolio ManagementThe Evolution of Microsoft Project Portfolio Management
The Evolution of Microsoft Project Portfolio Management
OnePlan Solutions
Wondershare Filmora Crack 2025 For Windows Free
Wondershare Filmora Crack 2025 For Windows FreeWondershare Filmora Crack 2025 For Windows Free
Wondershare Filmora Crack 2025 For Windows Free
mohsinrazakpa43
Lumion Pro Crack latest version Free 2025
Lumion Pro Crack latest version Free 2025Lumion Pro Crack latest version Free 2025
Lumion Pro Crack latest version Free 2025
naeem55ddf
Movavi Screen Recorder Studio 2025 crack Free Download
Movavi Screen Recorder Studio 2025 crack Free DownloadMovavi Screen Recorder Studio 2025 crack Free Download
Movavi Screen Recorder Studio 2025 crack Free Download
imran03kr
Virtual DJ Pro Crack 2025 Full Version Download [Latest]
Virtual DJ Pro Crack 2025 Full Version Download [Latest]Virtual DJ Pro Crack 2025 Full Version Download [Latest]
Virtual DJ Pro Crack 2025 Full Version Download [Latest]
umeerbinfaizan
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odooTour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
AxisTechnolabs
Bandicut Video Cutter v3.6.8.709 Crack [April-2025]
Bandicut Video Cutter v3.6.8.709 Crack [April-2025]Bandicut Video Cutter v3.6.8.709 Crack [April-2025]
Bandicut Video Cutter v3.6.8.709 Crack [April-2025]
jackalen173
AI has already changed software development.pdf
AI has already changed software development.pdfAI has already changed software development.pdf
AI has already changed software development.pdf
Radam辿s Roriz
Byteexpo Call Center - Presentation.pptx
Byteexpo Call Center - Presentation.pptxByteexpo Call Center - Presentation.pptx
Byteexpo Call Center - Presentation.pptx
hmk11790
wAIred_VoxxedDaysAmsterdam_03042025.pptx
wAIred_VoxxedDaysAmsterdam_03042025.pptxwAIred_VoxxedDaysAmsterdam_03042025.pptx
wAIred_VoxxedDaysAmsterdam_03042025.pptx
SimonedeGijt
Adobe XD Crack Version 2025 Free Download
Adobe XD Crack Version 2025 Free DownloadAdobe XD Crack Version 2025 Free Download
Adobe XD Crack Version 2025 Free Download
mohsinrazakpa67
microsoft office 2019 crack free download
microsoft office 2019 crack free downloadmicrosoft office 2019 crack free download
microsoft office 2019 crack free download
mohsinrazakpa39
Choreo - The AI-Native Internal Developer Platform as a Service: Overview
Choreo - The AI-Native Internal Developer Platform as a Service: OverviewChoreo - The AI-Native Internal Developer Platform as a Service: Overview
Choreo - The AI-Native Internal Developer Platform as a Service: Overview
WSO2
Driver Genius 24 Crack 2025 License Key Free Download
Driver Genius 24 Crack 2025 License Key Free DownloadDriver Genius 24 Crack 2025 License Key Free Download
Driver Genius 24 Crack 2025 License Key Free Download
umeerbinfaizan
Oracle Database administration Security PPT
Oracle Database administration Security PPTOracle Database administration Security PPT
Oracle Database administration Security PPT
pshankarnarayan
Rights, Copyrights, and Licences for Software Engineering Research v1.0
Rights, Copyrights, and Licences for Software Engineering Research v1.0Rights, Copyrights, and Licences for Software Engineering Research v1.0
Rights, Copyrights, and Licences for Software Engineering Research v1.0
Yann-Ga谷l Gu辿h辿neuc
The Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
The Open-Closed Principle - Part 2 - The Contemporary Version - An IntroductionThe Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
The Open-Closed Principle - Part 2 - The Contemporary Version - An Introduction
Philip Schwarz
Wondershare Filmora 14.3.2.11147 crack
Wondershare Filmora   14.3.2.11147 crackWondershare Filmora   14.3.2.11147 crack
Wondershare Filmora 14.3.2.11147 crack
blouch51kp
Clip Studio Paint EX Download (Latest 2025)
Clip Studio Paint EX Download (Latest 2025)Clip Studio Paint EX Download (Latest 2025)
Clip Studio Paint EX Download (Latest 2025)
mohsinrazakpa79
The Open-Closed Principle - Part 1 - The Original Version
The Open-Closed Principle - Part 1 - The Original VersionThe Open-Closed Principle - Part 1 - The Original Version
The Open-Closed Principle - Part 1 - The Original Version
Philip Schwarz
The Evolution of Microsoft Project Portfolio Management
The Evolution of Microsoft Project Portfolio ManagementThe Evolution of Microsoft Project Portfolio Management
The Evolution of Microsoft Project Portfolio Management
OnePlan Solutions
Wondershare Filmora Crack 2025 For Windows Free
Wondershare Filmora Crack 2025 For Windows FreeWondershare Filmora Crack 2025 For Windows Free
Wondershare Filmora Crack 2025 For Windows Free
mohsinrazakpa43
Lumion Pro Crack latest version Free 2025
Lumion Pro Crack latest version Free 2025Lumion Pro Crack latest version Free 2025
Lumion Pro Crack latest version Free 2025
naeem55ddf
Movavi Screen Recorder Studio 2025 crack Free Download
Movavi Screen Recorder Studio 2025 crack Free DownloadMovavi Screen Recorder Studio 2025 crack Free Download
Movavi Screen Recorder Studio 2025 crack Free Download
imran03kr
Virtual DJ Pro Crack 2025 Full Version Download [Latest]
Virtual DJ Pro Crack 2025 Full Version Download [Latest]Virtual DJ Pro Crack 2025 Full Version Download [Latest]
Virtual DJ Pro Crack 2025 Full Version Download [Latest]
umeerbinfaizan
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odooTour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
Tour Booking, Booking Service, Tour Agents, Hotel Booking in odoo
AxisTechnolabs
Bandicut Video Cutter v3.6.8.709 Crack [April-2025]
Bandicut Video Cutter v3.6.8.709 Crack [April-2025]Bandicut Video Cutter v3.6.8.709 Crack [April-2025]
Bandicut Video Cutter v3.6.8.709 Crack [April-2025]
jackalen173
AI has already changed software development.pdf
AI has already changed software development.pdfAI has already changed software development.pdf
AI has already changed software development.pdf
Radam辿s Roriz

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, youll have fewer decisions to make and youll 鍖nd more of what you need is already built. Convention over Con鍖guration 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 鍖le 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. Gem鍖le 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 speci鍖ed in con鍖g/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 /con鍖ng/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 鍖les 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 鍖le 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 鍖les, 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