rails3 for rails2 developers

31
Rails 3 For Rails 2 Developers

Upload: alkeshv

Post on 18-May-2015

5.672 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Rails3 for Rails2 developers

Rails 3

For Rails 2 Developers

Page 2: Rails3 for Rails2 developers

Components

Page 3: Rails3 for Rails2 developers

Lifecycle

Page 4: Rails3 for Rails2 developers

Controller Stack

Page 5: Rails3 for Rails2 developers

Creating a Rails 3 app

• rvm gemset create rails3• rvm gemset use rails3• gem install rails --pre• rails new test_app

Page 6: Rails3 for Rails2 developers

Scripts

Page 7: Rails3 for Rails2 developers

Look at:

• environment.rb• application.rb• config.ru

Page 8: Rails3 for Rails2 developers

Routes - compatibility

Old StyleTestApp::Application.routes.draw do |map|

map.resources :posts do |post| post.resources :commentsend

end

New StyleTestApp::Application.routes.draw do |map|resources :posts

doresources :comments

endend

Page 9: Rails3 for Rails2 developers

Routes - resources

Old Stylepost.resources :comments,

:member => { :preview => :post },:collection => { :archived => :get }

New Styleresources :comments do

post :preview, :on => :member get :archived, :on => :collection

end

Page 10: Rails3 for Rails2 developers

Routes – named routes

Old Stylemap.connect 'login’,

:controller => 'session', :action => 'new'map.login 'login’,

:controller => 'session', :action => 'new’

New Stylematch 'login' => 'session#new’match 'login' => 'session#new', :as => :login

Page 11: Rails3 for Rails2 developers

Routes – root paths

Old Stylemap.root :controller => 'users’, :action => 'index’

New Styleroot :to => 'users#index'

Page 12: Rails3 for Rails2 developers

Routes - Rack Endpoints

get 'hello' => proc { |env| [200, {}, "Hello Rack"] }

get 'rack_endpoint' => PostsController.action(:index)

get 'rack_app' => CustomRackApp

Page 13: Rails3 for Rails2 developers

Routes

Etc.Documentation here:http://guides.rails.info/routing.html

Page 14: Rails3 for Rails2 developers

ActionController – Legacy Styleclass UsersController < ApplicationController

def index@users = User.allrespond_to do |format| format.html format.xml { render :xml => @users.to_xml }end

enddef show

@user = User.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @user }

end endend

Page 15: Rails3 for Rails2 developers

ActionController – New Styleclass UsersController < ApplicationController

respond_to :html, :xml, :json

def index @users = User.all

respond_with(@users)end

def show@user = User.find(params[:id])respond_with(@user)

endend

Page 16: Rails3 for Rails2 developers

ActionMailer

def welcome(user, subdomain) @user = user @subdomain = subdomainmail(:from => [email protected],

:to => user.email,:subject => "Welcome to TestApp")

End

UserMailer.welcome(user, subdomain).deliver

Page 17: Rails3 for Rails2 developers

ActionMailerclass UserMailer < ActionMailer::Base

default :from => "[email protected]", :reply_to => "[email protected]", "X-Time-Code" => Time.now.to_i.to_s

def welcome(user, subdomain) @user = user@subdomain = subdomainattachments['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

endend

Page 18: Rails3 for Rails2 developers

ActiveRelation

Rails2@posts = Post.find(:all, :conditions => {:published => true})

@posts = Post.find(:all, :conditions => {:author => "Joe"}, :includes => :comments, :order => "title", :limit => 10)

Rails3@posts = Post.where(:published => true)@posts = Post.where(:author =>

"Joe").include(:comments).order(:title).limit(10).all

Page 19: Rails3 for Rails2 developers

ActiveRelation - scopes

Rails2class Post < ActiveRecord::Base

default_scope :order => 'title’named_scope :published, :conditions => {:published => true}named_scope :unpublished, :conditions => {:published => false}

end

Rails3class Post < ActiveRecord::Base

default_scope order('title')scope :published, where(:published => true)scope :unpublished, where(:published => false)

end

Page 20: Rails3 for Rails2 developers

ActiveRelation – new finder methods

• where (:conditions)• having (:conditions)• select• group• order• limit• offset• joins• includes (:include)• lock• readonly• from

Page 21: Rails3 for Rails2 developers

ActiveModel

• Attribute methods• Callbacks• Dirty• Errors• Naming• Observing• Serialization• Translation• Validations

Page 22: Rails3 for Rails2 developers

ActiveModel - example

class Applicant include ActiveModel::Validations validates_presence_of :name, :emailattr_accessor :name, :email

end

Page 23: Rails3 for Rails2 developers

XSS

Page 24: Rails3 for Rails2 developers

XSS

Rails2<%= @post.body %><%= h @post.body %>

Rails3<%= raw @post.body %> <%= @post.body.html_safe %><%= @post.body %>

Page 25: Rails3 for Rails2 developers

Unobtrusive Javascript

• HTML 5 custom data attributes: data-*– data-remote– data-method– data-confirm– data-disable-with

Page 26: Rails3 for Rails2 developers

Unobtrusive Javascript

Rails2<%= link_to_remote 'Show', :url => post %>

<a href="#" onclick="new Ajax.Request('/posts/1', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('9sk..44d')}); return false;">Show</a>

Rails3<%= link_to 'Show', post, :remote => true %><a href="/posts/1" data-remote="true">Show</a>

Page 27: Rails3 for Rails2 developers

Unobtrusive Javascript

Rails2<% remote_form_for(@post) do |f| %>

<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;">

Rails3<%= form_for(@post, :remote => true) do |f| %>

<form action="/posts" class="new_post" data-remote="true" id="new_post" method="post">

Page 28: Rails3 for Rails2 developers

Unobtrusive Javascript<%= link_to 'Destroy', post,

:confirm => 'Are you sure?', :method => :delete %>

Rails2<a href="/posts/1" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form');

f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);var s = 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>

Rails3<a href="/posts/1" data-method="delete" rel="nofollow">Destroy</a><a href="/posts/1" data-confirm="Are you sure?"

data-method="delete" rel="nofollow">Destroy</a>

Page 29: Rails3 for Rails2 developers

Unobtrusive Javascript

Rails2<%= f.submit 'Create Post',

:disable_with => "Please wait..." %>

<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" />

Rails3<%= f.submit :disable_with => "Please wait..." %>

<input data-disable-with="Please wait..." id="post_submit" name="commit" type="submit" value="Create Post" />

Page 30: Rails3 for Rails2 developers

Unobtrusive Javascript

Deprecated Methods:• link_to_remote • remote_form_for• observe_field• observe_form• form_remote_tag• button_to_remote• submit_to_remote• link_to_function• periodically_call_remote

Page 31: Rails3 for Rails2 developers

Links

• Rails Guides http://guides.rails.info/

• Rails3 Ropes coursehttp://en.oreilly.com/rails2010/public/schedule/detail/14137

• Reading Listhttp://mediumexposure.com/rails-3-reading-material/

• Screencasts http://rubyonrails.org/screencasts/rails3/