Rails 3<br />For Rails 2 Developers<br />
Components<br />
Lifecycle<br />
Controller Stack<br />
Creating a Rails 3 app<br />rvmgemset create rails3<br />rvmgemset use rails3<br />gem install rails --pre<br />rails new ...
Scripts<br />
Look at:<br />environment.rb<br />application.rb<br />config.ru<br />
Routes - compatibility<br />Old Style<br />TestApp::Application.routes.drawdo |map|<br />map.resources:posts do |post|post...
Routes - resources<br />Old Style<br />post.resources:comments,<br />:member => { :preview => :post },<br />:collection =>...
Routes – named routes<br />Old Style<br />map.connect'login’,<br />:controller => 'session', :action => 'new'<br />map.log...
Routes – root paths<br />Old Style<br />map.root:controller => 'users’,<br />:action => 'index’<br />New Style<br />root :...
Routes - Rack Endpoints<br />get 'hello' => proc { |env| [200, {}, "Hello Rack"] }<br />get 'rack_endpoint' => PostsContro...
Routes<br />Etc.<br />Documentation here:<br />http://guides.rails.info/routing.html<br />
ActionController – Legacy Style<br />class UsersController< ApplicationController<br />def index<br />@users = User.all<br...
ActionController – New Style<br />class UsersController< ApplicationControllerrespond_to:html, :xml, :json<br />def index<...
ActionMailer<br />def welcome(user, subdomain)<br />@user = user<br />@subdomain= subdomain<br />mail(:from=>admin@testapp...
ActionMailer<br />class UserMailer< ActionMailer::Base<br />default :from => "admin@testapp.com",<br />:reply_to=> "norepl...
ActiveRelation<br />Rails2<br />@posts = Post.find(:all, :conditions => {:published => true})<br />@posts = Post.find(:all...
ActiveRelation - scopes<br />Rails2<br />class Post < ActiveRecord::Base<br />default_scope:order => 'title’<br />named_sc...
ActiveRelation – new finder methods<br />where (:conditions)<br />having (:conditions)<br />select<br />group<br />order<b...
ActiveModel<br />Attribute methods<br />Callbacks<br />Dirty<br />Errors<br />Naming<br />Observing<br />Serialization<br ...
ActiveModel - example<br />class Applicant<br />include ActiveModel::Validationsvalidates_presence_of:name, :email<br />at...
XSS<br />
XSS<br />Rails2<br /><%=@post.body %><br /><%= h@post.body %><br />Rails3<br /><%= raw @post.body %> <%= @post.body.html_s...
Unobtrusive Javascript<br />HTML 5 custom data attributes: data-*<br />data-remote<br />data-method<br />data-confirm<br /...
Unobtrusive Javascript<br />Rails2<br /><%= link_to_remote'Show', :url=> post %><br /><a href="#" onclick="new Ajax.Reques...
Unobtrusive Javascript<br />Rails2<br /><% remote_form_for(@post) do |f| %><br /><form action="/posts" class="new_post" id...
Unobtrusive Javascript<br /><%= link_to'Destroy', post,<br />:confirm => 'Are you sure?', :method => :delete %><br />Rails...
Unobtrusive Javascript<br />Rails2<br /><%= f.submit'Create Post',<br />:disable_with=> "Please wait..." %><br /><input id...
Unobtrusive Javascript<br />Deprecated Methods:<br />link_to_remote<br />remote_form_for<br />observe_field<br />observe_f...
Links<br />Rails Guides http://guides.rails.info/<br />Rails3 Ropes coursehttp://en.oreilly.com/rails2010/public/schedule/...
Upcoming SlideShare
Loading in …5
×

Rails3 for Rails2 developers

5,964 views
5,838 views

Published on

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,964
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
45
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • 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 =&gt; &quot;home#index&quot;
  • 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.
  • First lines are unsafeSecond lines are safe
  • Rails3 for Rails2 developers

    1. 1. Rails 3<br />For Rails 2 Developers<br />
    2. 2. Components<br />
    3. 3. Lifecycle<br />
    4. 4. Controller Stack<br />
    5. 5. Creating a Rails 3 app<br />rvmgemset create rails3<br />rvmgemset use rails3<br />gem install rails --pre<br />rails new test_app<br />
    6. 6. Scripts<br />
    7. 7. Look at:<br />environment.rb<br />application.rb<br />config.ru<br />
    8. 8. Routes - compatibility<br />Old Style<br />TestApp::Application.routes.drawdo |map|<br />map.resources:posts do |post|post.resources:comments<br />end<br />end<br />New Style<br />TestApp::Application.routes.drawdo |map|resources:posts do<br />resources :comments<br />end<br />end<br />
    9. 9. Routes - resources<br />Old Style<br />post.resources:comments,<br />:member => { :preview => :post },<br />:collection => { :archived => :get }<br />New Style<br />resources :comments do<br />post :preview, :on => :member get :archived, :on => :collection<br />end<br />
    10. 10. Routes – named routes<br />Old Style<br />map.connect'login’,<br />:controller => 'session', :action => 'new'<br />map.login'login’,<br />:controller => 'session', :action => 'new’<br />New Style<br />match 'login' => 'session#new’<br />match 'login' => 'session#new', :as => :login<br />
    11. 11. Routes – root paths<br />Old Style<br />map.root:controller => 'users’,<br />:action => 'index’<br />New Style<br />root :to => 'users#index'<br />
    12. 12. Routes - Rack Endpoints<br />get 'hello' => proc { |env| [200, {}, "Hello Rack"] }<br />get 'rack_endpoint' => PostsController.action(:index)<br />get 'rack_app' => CustomRackApp<br />
    13. 13. Routes<br />Etc.<br />Documentation here:<br />http://guides.rails.info/routing.html<br />
    14. 14. ActionController – Legacy Style<br />class UsersController< ApplicationController<br />def index<br />@users = User.all<br />respond_todo |format|<br />format.html<br />format.xml{ render :xml => @users.to_xml}<br />end<br /> end<br /> def show<br />@user = User.find(params[:id])<br />respond_todo |format|<br />format.html# show.html.erb<br />format.xml{ render :xml => @user }<br /> end<br /> end<br />end<br />
    15. 15. ActionController – New Style<br />class UsersController< ApplicationControllerrespond_to:html, :xml, :json<br />def index<br />@users = User.all<br />respond_with(@users)<br />end<br />def show<br />@user = User.find(params[:id])<br />respond_with(@user)<br />end<br />end<br />
    16. 16. ActionMailer<br />def welcome(user, subdomain)<br />@user = user<br />@subdomain= subdomain<br />mail(:from=>admin@testapp.com,<br />:to => user.email,<br /> :subject => "Welcome to TestApp")<br />End<br />UserMailer.welcome(user, subdomain).deliver<br />
    17. 17. ActionMailer<br />class UserMailer< ActionMailer::Base<br />default :from => "admin@testapp.com",<br />:reply_to=> "noreply@testapp.com",<br />"X-Time-Code" => Time.now.to_i.to_s<br />def welcome(user, subdomain)<br />@user = user<br />@subdomain= subdomain<br />attachments['test.pdf'] =<br />File.read("#{Rails.root}/public/test.pdf")<br />mail(:to=> @user.email, :subject => "Welcome to TestApp") do |format|format.html{ render 'other_html_welcome' }<br />format.text{ render 'other_text_welcome' }<br />end <br /> end<br />end<br />
    18. 18. ActiveRelation<br />Rails2<br />@posts = Post.find(:all, :conditions => {:published => true})<br />@posts = Post.find(:all, :conditions => {:author => "Joe"}, :includes => :comments, :order => "title", :limit => 10)<br />Rails3<br />@posts = Post.where(:published=> true)<br />@posts = Post.where(:author=> "Joe").include(:comments).order(:title).limit(10).all<br />
    19. 19. ActiveRelation - scopes<br />Rails2<br />class Post < ActiveRecord::Base<br />default_scope:order => 'title’<br />named_scope:published, :conditions => {:published => true}<br />named_scope:unpublished, :conditions => {:published => false}<br />end<br />Rails3<br />class Post < ActiveRecord::Base<br />default_scopeorder('title')<br /> scope :published, where(:published=> true)scope:unpublished, where(:published=> false)<br />end<br />
    20. 20. ActiveRelation – new finder methods<br />where (:conditions)<br />having (:conditions)<br />select<br />group<br />order<br />limit<br />offset<br />joins<br />includes (:include)<br />lock<br />readonly<br />from<br />
    21. 21. ActiveModel<br />Attribute methods<br />Callbacks<br />Dirty<br />Errors<br />Naming<br />Observing<br />Serialization<br />Translation<br />Validations<br />
    22. 22. ActiveModel - example<br />class Applicant<br />include ActiveModel::Validationsvalidates_presence_of:name, :email<br />attr_accessor:name, :email<br />end<br />
    23. 23. XSS<br />
    24. 24. XSS<br />Rails2<br /><%=@post.body %><br /><%= h@post.body %><br />Rails3<br /><%= raw @post.body %> <%= @post.body.html_safe %><br /><%= @post.body %><br />
    25. 25. Unobtrusive Javascript<br />HTML 5 custom data attributes: data-*<br />data-remote<br />data-method<br />data-confirm<br />data-disable-with<br />
    26. 26. Unobtrusive Javascript<br />Rails2<br /><%= link_to_remote'Show', :url=> post %><br /><a href="#" onclick="new Ajax.Request('/posts/1', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('9sk..44d')}); return false;">Show</a><br />Rails3<br /><%= link_to'Show', post, :remote => true %><br /><a href="/posts/1" data-remote="true">Show</a><br />
    27. 27. Unobtrusive Javascript<br />Rails2<br /><% remote_form_for(@post) do |f| %><br /><form action="/posts" class="new_post" id="new_post" method="post" onsubmit="new Ajax.Request('/posts', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;"><br />Rails3<br /><%= form_for(@post, :remote => true) do |f| %><br /><form action="/posts" class="new_post" data-remote="true" id="new_post" method="post"><br />
    28. 28. Unobtrusive Javascript<br /><%= link_to'Destroy', post,<br />:confirm => 'Are you sure?', :method => :delete %><br />Rails2<br /><a href="/posts/1" onclick="if (confirm('Are you sure?')) { varf = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;varm = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);vars = document.createElement('input'); s.setAttribute ('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', '9skdJ0k+l9/ q3PWToz6MtfyiB2gcyhnKubeGV6WFL44='); f.appendChild(s);f.submit(); };return false;">Destroy</a><br />Rails3<br /><a href="/posts/1" data-method="delete" rel="nofollow">Destroy</a><br /><a href="/posts/1" data-confirm="Are you sure?"<br />data-method="delete" rel="nofollow">Destroy</a><br />
    29. 29. Unobtrusive Javascript<br />Rails2<br /><%= f.submit'Create Post',<br />:disable_with=> "Please wait..." %><br /><input id="post_submit" name="commit" onclick="if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit = document.createElement('input');hiddenCommit.type = 'hidden';hiddenCommit.value = this.value;hiddenCommit.name = this.name;this.form.appendChild(hiddenCommit); } this.setAttribute('originalValue', this.value);this.disabled = true;this.value='Please wait...';result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());if (result == false) { this.value = this.getAttribute ('originalValue');this.disabled = false; }return result;" type="submit" value="Create Post" /><br />Rails3<br /><%= f.submit:disable_with=> "Please wait..." %><br /><input data-disable-with="Please wait..." id="post_submit" name="commit" type="submit" value="Create Post" /><br />
    30. 30. Unobtrusive Javascript<br />Deprecated Methods:<br />link_to_remote<br />remote_form_for<br />observe_field<br />observe_form<br />form_remote_tag<br />button_to_remote<br />submit_to_remote<br />link_to_function<br />periodically_call_remote<br />
    31. 31. Links<br />Rails Guides http://guides.rails.info/<br />Rails3 Ropes coursehttp://en.oreilly.com/rails2010/public/schedule/detail/14137<br />Reading Listhttp://mediumexposure.com/rails-3-reading-material/<br />Screencastshttp://rubyonrails.org/screencasts/rails3/<br />

    ×