Pourquoi Ruby on Rails ça déchire ?

35
Simon Courtois - @happynoff www.tinci.fr Ruby on Rails Why does it rock?

description

Présentation accompagnant mon talk du Meetup Paris.rb du 10 janvier 2012.

Transcript of Pourquoi Ruby on Rails ça déchire ?

Page 1: Pourquoi Ruby on Rails ça déchire ?

Simon Courtois - @happynoff

www.tinci.fr

Ruby on Rails Why does it rock?

Page 2: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Web Development

Software Development

X_

Consulting & Support

@tincihq

Page 3: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

M V C

Page 4: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Model View Controller

Page 5: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

O R M

Page 6: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Object Relational Mapping

Page 7: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Active Recordarticles

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"

Page 8: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Active Recordarticles

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

Page 9: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Conventions Configuration

Page 10: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Active Recordarticles

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”

Page 11: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Active Record

MySQL PostgreSQL

SQLite …

Page 12: Pourquoi Ruby on Rails ça déchire ?

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 verbcontroller

controller’s action

parameter

Page 13: Pourquoi Ruby on Rails ça déchire ?

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 !

Page 14: Pourquoi Ruby on Rails ça déchire ?

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>

Page 15: Pourquoi Ruby on Rails ça déchire ?

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

Page 16: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Railties

$ rake routesGET /hello/:name { :controller => "hello", :action => "index" }

$ rails serverStarts a web server listening on http://localhost:3000/

$ rails consoleStarts an interactive console in the application context >> Article.first.title! #=> "hello world"

Page 17: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Generators

$ rails generate model author name:stringinvoke active_record create db/migrate/20120108151543_create_authors.rb create app/models/author.rb

instructions to create the authors table

the Author model

Page 18: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Generators

$ rails g scaffold author name:stringcreate 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

modelroutes

controller

views

default CSS

Page 19: Pourquoi Ruby on Rails ça déchire ?

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

Page 20: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Generators

Page 21: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Generators

Page 22: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Generators

Page 23: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Generators

Page 24: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Generators

Page 25: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Generators

Page 26: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Generators

Page 27: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Generators

Page 28: Pourquoi Ruby on Rails ça déchire ?

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"

Page 29: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

ExtensibilityThanks to gems

Page 30: Pourquoi Ruby on Rails ça déchire ?

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

Page 31: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

And many more

MongoidKaminari

Carrierwave

Active Admin

MongoDB

Pagination

File upload

Administration interface

Page 32: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Who is using Rails?

Twitter

BasecampGithub

GrouponUS Yellow Pages

Shopify

http://rubyonrails.org/applications

Page 33: Pourquoi Ruby on Rails ça déchire ?

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

Page 34: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Questions?

Page 35: Pourquoi Ruby on Rails ça déchire ?

www.tinci.fr

Thanks!