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 :) )
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)
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