This document provides an overview of the key changes in migrating from Rails 2 to Rails 3. It discusses updates to components like routing, controllers, models and views. Specific changes include moving to RESTful routing, removing deprecated methods like link_to_remote, and adopting unobtrusive JavaScript. Links are provided for additional documentation on routing, controllers, mailers and other aspects of the Rails 3 framework.
8. Routes - compatibility Old Style TestApp::Application.routes.drawdo |map| map.resources:posts do |post|post.resources:comments end end New Style TestApp::Application.routes.drawdo |map|resources:posts do resources :comments end end
9. Routes - resources Old Style post.resources:comments, :member => { :preview => :post }, :collection => { :archived => :get } New Style resources :comments do post :preview, :on => :member get :archived, :on => :collection end
10. Routes – named routes Old Style map.connect'login’, :controller => 'session', :action => 'new' map.login'login’, :controller => 'session', :action => 'new’ New Style match 'login' => 'session#new’ match 'login' => 'session#new', :as => :login
11. Routes – root paths Old Style map.root:controller => 'users’, :action => 'index’ New Style root :to => 'users#index'
12. Routes - Rack Endpoints get 'hello' => proc { |env| [200, {}, "Hello Rack"] } get 'rack_endpoint' => PostsController.action(:index) get 'rack_app' => CustomRackApp
14. ActionController – Legacy Style class UsersController< ApplicationController def index @users = User.all respond_todo |format| format.html format.xml{ render :xml => @users.to_xml} end end def show @user = User.find(params[:id]) respond_todo |format| format.html# show.html.erb format.xml{ render :xml => @user } end end end
15. ActionController – New Style class UsersController< ApplicationControllerrespond_to:html, :xml, :json def index @users = User.all respond_with(@users) end def show @user = User.find(params[:id]) respond_with(@user) end end
16. ActionMailer def welcome(user, subdomain) @user = user @subdomain= subdomain mail(:from=>admin@testapp.com, :to => user.email, :subject => "Welcome to TestApp") End UserMailer.welcome(user, subdomain).deliver
17. ActionMailer class UserMailer< ActionMailer::Base default :from => "admin@testapp.com", :reply_to=> "noreply@testapp.com", "X-Time-Code" => Time.now.to_i.to_s def welcome(user, subdomain) @user = user @subdomain= subdomain attachments['test.pdf'] = File.read("#{Rails.root}/public/test.pdf") mail(:to=> @user.email, :subject => "Welcome to TestApp") do |format|format.html{ render 'other_html_welcome' } format.text{ render 'other_text_welcome' } end end end
19. ActiveRelation - scopes Rails2 class Post < ActiveRecord::Base default_scope:order => 'title’ named_scope:published, :conditions => {:published => true} named_scope:unpublished, :conditions => {:published => false} end Rails3 class Post < ActiveRecord::Base default_scopeorder('title') scope :published, where(:published=> true)scope:unpublished, where(:published=> false) end
20. ActiveRelation – new finder methods where (:conditions) having (:conditions) select group order limit offset joins includes (:include) lock readonly from
Rails components decoupled from each otherEach component has a clear, published API.
Action dispatch – new – handles: -Request handling and parameter parsing -Sessions, Rails’ flash, and cookie storage -File uploads -Routing, URL matching, and rescuing errors -HTTP conditional GETs -Client response and HTTP status code
AbstractController Base: rendering, layouts, managing template pathsActionController Metal: exposes Rack endpointsActionController Base: like the rails2 one.
If you’re mistakenly using rails2 – you’ll end up with an app called ‘new’.
rake db:createrails srails g controller home indexrails g scaffold Post name:stringtitle:stringcontent:text
environment.rb – much simplerapplication.rb – new!config.ru – yes, it’s a rack app
Old style still works in rails3
Makes named routes more obvious
Rails 3 example from http://guides.rails.info/getting_started.html:root :to => "home#index"
Still works – just ugly
rails g mailer UserMailer welcome forgot_passwordCreates user_mailer in app/mailers
Rails2 find used to query the database.Rails3 returns an ActiveRecord::Relation – only queried when you do @posts.eachThe .all on the last query, forces the query to be evaluated, and so returns an array. Without it, you’d get an ActiveRelation object.