Simon Courtois - @happynoff
www.tinci.fr
Ruby on Rails
Why does it rock?
www.tinci.fr
Web
Development
Software
Development
X_
Consulting &
Support
@tincihq
www.tinci.fr
M V C
www.tinci.fr
Model View Controller
www.tinci.fr
O R M
www.tinci.fr
Object Relational Mapping
www.tinci.fr
Active Record
articles
id title body
1 hello world This is a body
# app/models/article.rb
class Article < ActiveRecord::Base
end
!
article = Article.first
!
article.title
#=> "hello world"
www.tinci.fr
Active Record
articles
id title body published
1 hello world This is a body 1
2 other art. Not published 0
articles = Article.where(published: 1)
!
articles.count
#=> 1
www.tinci.fr
Conventions
Configuration
www.tinci.fr
Active Record
articles
id title body author_id
1 ... ... 1
authors
id name
1 John Doe
# app/models/article.rb
class Article < ActiveRec...
belongs_to :author
end
!
# app/models/author.rb
class Author < ActiveRec...
has_many :articles
end
!
article = Article.first
!
article.author.name
#=> “John Doe”
www.tinci.fr
Active Record
MySQL
PostgreSQL
SQLite
…
www.tinci.fr
Routing
# app/controller/hello_controller.rb
class HelloController < ApplicationController
def index
@name = params[:name]
end
end
http://example.com/hello/John
# config/routes.rb
get "hello/:name" => "hello#index"
URL
HTTP verb
controller
controller’s action
parameter
www.tinci.fr
Views
# app/controller/hello_controller.rb
class HelloController < ApplicationController
def index
@name = params[:name]
end
end
# app/views/hello/index.html.erb
Hello <%= @name %>
Conventions !
www.tinci.fr
Helpers
# app/views/articles/index.html.erb
<%= @articles.each do |article| %>
<p><%= link_to article.title, article %></p>
<% end %>
# app/controller/articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
end
<p><a href="/articles/1">hello world</a></p>
www.tinci.fr
Helpers
# app/controller/articles_controller.rb
class ArticlesController < ApplicationController
def new
@article = Article.new
end
end
<%= form_for @article do |f| %>
<p><%= f.label :title, "Title" %><br />
<%= f.text_field :title %></p>
!
<p><%= f.label :body, "Body" %><br />
<%= f.text_area :body %></p>
!
<p><%= f.submit %></p>
<% end %>
Title
Body
Create Article
www.tinci.fr
Railties
$ rake routes
GET /hello/:name { :controller => "hello", :action => "index" }
$ rails server
Starts a web server listening on http://localhost:3000/
$ rails console
Starts an interactive console in the application context
>> Article.first.title!
#=> "hello world"
www.tinci.fr
Generators
$ rails generate model author name:string
invoke active_record	

create db/migrate/20120108151543_create_authors.rb	

create app/models/author.rb
instructions to create the authors table
the Author model
www.tinci.fr
Generators
$ rails g scaffold author name:string
create db/migrate/20120108152723_create_authors.rb	

create app/models/author.rb	

!
route resources :authors	

!
create app/controllers/authors_controller.rb	

!
create app/views/authors/index.html.erb	

create app/views/authors/edit.html.erb	

create app/views/authors/show.html.erb	

create app/views/authors/new.html.erb	

create app/views/authors/_form.html.erb	

!
create public/stylesheets/scaffold.css
model
routes
controller
views
default CSS
www.tinci.fr
Generators
# config/routes.rb
resources :authors
authors
 
 GET
 
 /authors

 
 
 { action: index controller: authors }
author
 
 GET
 
 /authors/:id

 
 { action: show controller: authors }
new_author
GET
 
 /authors/new
 
 { action: new controller: authors }

 
 
 
 POST
 
 /authors

 
 
 { action: create controller: authors }
edit_author
GET
 
 /authors/:id/edit
 { action: edit controller: authors }

 
 
 
 PATCH
 /authors/:id

 
 { action: update controller: authors }

 
 
 
 PUT
 
 /authors/:id

 
 { action: update controller: authors }

 
 
 
 DELETE
 /authors/:id

 
 { action: destroy controller: authors }
<%= link_to "All authors", authors_path %>
<%= link_to "Edit", edit_author_path(@author) %>
www.tinci.fr
Generators
www.tinci.fr
Generators
www.tinci.fr
Generators
www.tinci.fr
Generators
www.tinci.fr
Generators
www.tinci.fr
Generators
www.tinci.fr
Generators
www.tinci.fr
Generators
www.tinci.fr
ActiveSupport little additions
1.kilobytes! #=> 1024!
!
!
3.days.ago!! #=> Tue, 24 Jun 2014 09:44:47 UTC +00:00!
!
!
"héhé test".parameterize! #=> "hehe-test"!
!
!
“article”.pluralize!! ! ! #=> "articles"
www.tinci.fr
Extensibility
Thanks to gems
www.tinci.fr
Views and forms
<%= form_for @article do |f| %>
<p><%= f.label :title, "Title" %><br />
<%= f.text_field :title %></p>
!
<p><%= f.label :body, "Body" %><br />
<%= f.text_area :body %></p>
!
<p><%= f.submit %></p>
<% end %>
= simple_form_for @article do |f|

 = f.input :title

 = f.input :body

 = f.submit
slim + simple_form
www.tinci.fr
And many more
Mongoid
Kaminari
Carrierwave
Active Admin
MongoDB
Pagination
File upload
Administration interface
www.tinci.fr
Who is using Rails?
Twitter
Basecamp
Github
Groupon
US Yellow Pages
Shopify
http://rubyonrails.org/applications
www.tinci.fr
Resources
rubyonrails.org Official website
tutorials.jumpstartlab.com/topics Very complete tutorial
railsforzombies.org Interactive tutorial
railstutorial.org/book Free online book
#rubyonrails.fr French IRC channel
www.tinci.fr
Questions?
www.tinci.fr
Thanks!

Pourquoi Ruby on Rails ça déchire ?