TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
Rails 3: Les nouveautés de la commande rails et d'ActiveRelation
1. Les nouveautés de Rails 3
Simon COURTOIS
Expert en Open-Source
scourtois@linagora.com
1 WWW.LINAGORA.COM
2. La commande rails
Rails 2 Rails 3
rails myapp rails new myapp
./script/generate rails g
./script/console rails c
./script/server rails s
./script/dbconsole rails db
2 /25
11. ActiveRelation
where(:conditions) all
having(:conditions) first
select last
group
order
limit
offset
joins
includes(:include)
lock
readonly
from
11/25
13. ActiveController
Rails 2 Rails 3
class ArticlesController < ApplicationController class ArticlesController < Applic...
def index respond_to :html, :xml
@users = User.all
def index
respond_to do |format| @users = User.all
format.html respond_with(@users)
format.xml { render :xml => @users.to_xml } end
end
end def show
@user = User.find(params[:id])
def show respond_with(@user)
@user = User.find(params[:id]) end
end
respond_to do |format|
format.html
format.xml { render :xml => @user }
end
end
end
flash[:notice] = “Article created” redirect_to @article,
redirect_to @article :notice => “Article created”
13/25
14. Le routing
Rails 2 Rails 3
ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map|
map.resources :articles resources :articles
end end
14/25
15. Le routing
Rails 2 Rails 3
ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map|
map.resources :articles, resources :articles do
:member => { :preview => :post }, member do
:collection => { :archived => :get } post :preview
end end
collection do
get :archived
end
end
end
Myapp::Application.routes.draw do |map|
resources :articles do
post :preview, :on => :member
get :archived, :on => :collection
end
end
15/25
16. Le routing
Rails 2 Rails 3
ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map|
map.resources :articles do |article| resources :articles do
article.resources :comments resources :comments
end end
end end
ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map|
map.connect “login”, match “login” => “session#new”
:controller => “session”, end
:action => “new”
end
16/25
17. Le routing
Rails 2 Rails 3
ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map|
map.login “login”, match “login” => “session#new”, :as => :login
:controller => “session”, end
:action => “new”
end
ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map|
map.root :controller => “articles”, root :to => “users#index”
:action => “index” end
end
ActionController::Routing::Routes.draw do |map| Myapp::Application.routes.draw do |map|
map.connect “:controller/:action/:id” match “:controller(/:action(/:id(.:format)))”
map.connect “:controller/:action/:id.:format” end
end
17/25
18. Le routing
Rails 2
ActionController::Routing::Routes.draw do |map|
map.connect ‘/articles/:year/:month/:day’, :controller => “articles”, :action => “index”
map.connect ‘/articles/:year/:month’, :controller => “articles”, :action => “index”
map.connect ‘/articles/:year’, :controller => “articles”, :action => “index”
end
Rails 3
Myapp::Application.routes.draw do |map|
match “/articles(/:year(/:month(/:day)))” => “articles#index”
end
18/25
19. Le routing
Rails 2
ActionController::Routing::Routes.draw do |map|
map.connect ‘/articles/:year’, :controller => “articles”, :action => “index”,
:conditions => { :method => :get }
end
Rails 3
Myapp::Application.routes.draw do |map|
match “/articles/:year” => “articles#index”, :via => :get
end
Myapp::Application.routes.draw do |map|
get “/articles/:year” => “articles#index”
end
19/25
20. Le routing
Rails 3
Myapp::Application.routes.draw do |map|
match “signin”, :to => redirect(“/login”)
match “linagora”, :to => redirect(“http://www.linagora.com”)
end
Myapp::Application.routes.draw do |map|
get “hello” => proc { |env| [200, {}, “Hello World !”] }
get “rack_app” => MyCoolRackApp
end
RAILS_ROOT/lib/my_cool_rack_app.rb
20/25
28. Merci de votre attention
Cont act :
LINAGORA - Siège social
80, rue Roque de Fillol
92800 PUTEAUX
FRANCE
Tél. : 0 810 251 251 (t arif local)
Fax : +33 (0)1 46 96 63 64
Mail : info@linagora.com
Web : www.linagora.com
28 WWW.LINAGORA.COM