1. Trang chủ
  2. » Kinh Doanh - Tiếp Thị

Rails 3 Ropes Course: Gregg Pollack ppt

86 292 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Rails 3 Ropes Course: Gregg Pollack ppt
Tác giả Gregg Pollack, Nathaniel Bibler, Thomas Meeks, Jacob Swanner, Tyler Hunt, Mark Kendall, Caike Souza
Trường học University of [Insert University Name]
Chuyên ngành Web Development
Thể loại lecture notes
Định dạng
Số trang 86
Dung lượng 4,96 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

module TestApp class Application < Rails::Application end end It’s a Rack Application... TestApp :: Application .routes.draw do | map |end config/routes.rb New Routing API Rails 3... N

Trang 1

Rails 3 Ropes Course

Trang 2

Getting Started & Routes

Trang 3

Starting a New App Usage:

Options:

.bundle db/*.sqlite3 log/*.log

tmp/**/*

.gitignore

Trang 7

Rails :: Initializer run do | config |

config gem "bj"

config gem "sqlite3-ruby" , :lib => "sqlite3"

config gem "aws-s3" , :lib => "aws/s3"

Gemfile

Trang 8

module TestApp

class Application < Rails::Application

end end

It’s a Rack Application

Trang 9

New Router API

Trang 10

http_auth helpers

Trang 11

TestApp :: Application routes.draw do | map |

end

config/routes.rb

New Routing API

Rails 3

Trang 12

TestApp :: Application routes.draw do | map |

end

config/routes.rb

resources :posts

map.

New Routing API

Old routing syntax works

Rails 3

Trang 13

map.resources :posts do | post |

Trang 14

post.resources :comments ,

:member => { :preview => :post },

:collection => { :archived => :get }

post :preview , :on => :member

get :archived , :on => :collection

Trang 15

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

match 'login' => 'session#new'

Rails 2

Rails 3

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

match 'login' => 'session#new' , :as => :login

Rails 2

Rails 3

Named Route login_path

Trang 16

map.root :controller => 'users' , :action => 'index'

Trang 17

map.connect '/articles/:year/:month/:day' , :controller => 'posts' , :action => 'index'

Rails 2

Rails 3

match '/articles/:year/:month/:day' => "posts#index"

Optional Parameters

map.connect '/articles/:year/:month/:day' , :controller => 'posts' , :action => 'index'

map.connect '/articles/:year/:month' , :controller => 'posts' , :action => 'index'

map.connect '/articles/:year' , :controller => 'posts' , :action => 'index'

Rails 2

Rails 3

match '/articles(/:year(/:month(/:day)))' => "posts#index"

Trang 18

map.connect '/articles/:year' , :controller => 'posts' , :action => 'index' , :conditions => { :method => :get }

Rails 2

Rails 3

match '/articles/:year' => "posts#index" , :via => :get

Specifying the method

Rails 3

get '/articles/:year' => "posts#index"

Trang 19

Rails 3

match 'signin' , :to => redirect( "/login" )

Redirection

match 'users/:name' , :to => redirect {| params | "/ #{params[ :name ]} " }

match 'google' => redirect( 'http://www.google.com/' )

Trang 20

map.connect '/:year' , :controller => 'posts' , :action => 'index' ,

Trang 21

Rails 3

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

Rack Goodness

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

get 'rack_app' => CustomRackApp

Trang 22

For more information

Trang 23

Getting Started & Routes

Tutorial - Lab #1

Follow the directions in the README

Trang 24

Getting Started & Routes

Trang 25

Dependency Management

Trang 26

Typical Rails deployment

config gem "haml"

config gem "chronic" , :version => '0.2.3'

:dependencies

Trang 27

Issues

Conflicts occur at runtime

Not great dependency resolution It’s bound into Rails

Trang 28

Dependency Resolution

Gem::LoadError: can't activate activesupport (= 2.3.2,

runtime), already activated activesupport-2.3.3

Trang 29

Bundler Commands

gem "haml"

gem "chronic" , '0.2.3'

Trang 30

With Bundler

Happy!

Trang 32

Gemfile Syntax

source "http://rubygems.org"

gem "hpricot" , "0.6"

gem "sqlite3-ruby" , :require => "sqlite3"

gem "rails" , :git => "git://github.com/rails/rails.git"

gem "rails" , :path => "~/Sites/rails"

$bundle without test

Will ensure all gems are installed, including webrat (but it’s only included in test mode) Will install everything except webrat.

Trang 34

I locally run

When I download a new application

When I deploy my application

I run on the server

When my application is going live

on the server

Trang 35

Gemfile vs Gemfile.lock

Gemfile

Gemfile.lock

nokogiri 1.4.1 released!

GEM

'capistrano'

Trang 36

On the server Run locally

But I don’t want to rely on external servers for deployment

Trang 37

For more information

Trang 38

AbstractController::Base

Assigns Callbacks Collector Helpers Layouts Logger Rendering Translation ViewPaths

ActionController::Base

Cookies Exceptions Flash Helpers Redirecting Rendering Responder UrlFor (lots more)

abstract

abstract

abstract

AbstractController Namespace ActionController Namespace

includes

Trang 39

class UsersController < ApplicationController

Trang 40

class UsersController < ApplicationController

Trang 41

Bundler & ActionController

Tutorial - Lab #2

Trang 42

Getting Started & Routes

Trang 43

ActionMailer Mikel Lindsaar

Trang 44

New ActionMailer Syntax

Rails 2

Rails 3

mailers /user_mailer.rb

Trang 45

New ActionMailer Syntax

Rails 2

Rails 3

def welcome ( user, subdomain )

subject 'Welcome to TestApp'

UserMailer deliver_welcome(user, subdomain)

UserMailer welcome(user, subdomain).deliver

Trang 46

New ActionMailer Syntax

format.text { render 'other_text_welcome' }

end

default :from => "admin@testapp.com" ,

welcome.text.erb welcome.html.erb

Defaults

:reply_to => "noreply@testapp.com" ,

"X-Time-Code" => Time now.to_i.to_s

Trang 47

Tutorial - Step #3

Trang 48

Getting Started & Routes

Trang 49

replaces the internal ad-hoc query generation with query generation based on relational algebra.

Nick Kallen

Trang 50

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

@posts = Post where( :published => true )

Trang 51

@posts = Post where( :published => true )

Trang 52

@posts = Post where( :published => true )

if params[ :order ]

@posts = @posts order(params[ :order ])

end

@posts = Post where( :published => true )

@posts = @posts order(params[ :order ])

@posts = Post where( :published => true ).order(params[:order])

ActiveRelation

Trang 53

@posts = Post where( :published => true ).order(params[:order])

posts = Post order(params[ :order ])

@published = posts.where( :published => true )

@unpublished = posts.where( :published => false )

This is obviously a bad example

@published = Post.published

@unpublished = Post.unpublished

ActiveRelation

Trang 54

@published = Post.published

@unpublished = Post.unpublished

@published = Post.published

@unpublished = Post.unpublished

class Post < ActiveRecord::Base

default_scope :order => 'title'

named_scope :published , :conditions => { :published => true }

named_scope :unpublished , :conditions => { :published => false }

end

class Post < ActiveRecord::Base

default_scope order( 'title' )

scope :published , where( :published => true )

scope :unpublished , where( :published => false )

end

Rails 2

Rails 3

ActiveRelation

Trang 56

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

Post where(:author => "Joe" ) include (:comments).order(:title).limit(10) all

ActiveRelation

Rails 2

Rails 3

Trang 57

ActiveModel API

ActiveModel Helper Modules

Trang 58

Old ActiveRecord Stack

Dirty Errors Naming Observing Serialization Translation Validations

Trang 59

Attribute Methods Callbacks

Dirty Errors Naming Observing Serialization Translation Validations

validates_length_of :name , :within => 3 20

validates_inclusion_of :salary , :in => 50000 200000

person = Person new

person.serializable_hash

person.to_json

person.to_xml

Trang 60

Attribute Methods Callbacks

Trang 61

class Applicant

include ActiveModel :: Validations

validates_presence_of :name , :email

attr_accessor :name , :email

Trang 63

ActiveRelation & ActiveModel

Tutorial - Lab #4

Trang 64

Getting Started & Routes

Trang 65

Cross Site Scripting

XSS

Trang 67

<%= raw @post body %>

Returns a String which is assumed to be

safe and will not be escaped again

Trang 68

Cross-Site Scripting (XSS)

Rails 2

Rails 3

<%= link_to "<span class='cart'>Cart</span>" , cart_path %>

<%= link_to raw( " <span class='cart'>Cart</span> " ), cart_path %>

Rails 3

<%= link_to raw( "<span class='cart'> #{@user_input} </span>" ), cart_path %>

No escaping done on user input

Vulnerable to attack!!

Trang 69

Cross-Site Scripting (XSS)

<%= link_to raw( "<span class='cart'> #{@user_input} </span>" ), cart_path %>

<%= link_to raw( "<span class='cart'> #{h @user_input} </span>" ), cart_path %>

Safe!

Trang 70

Unobtrusive Javascript

UJS

Trang 71

HTML 5 custom data attributes

Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements

data-*

Trang 72

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

<%= link_to 'Show' , post, :remote => true %>

<a href= "#" onclick= "new Ajax.Request('/posts/1', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' +

encodeURIComponent('9sk 44d')}); return false;" > Show </a>

<a href= "/posts/1" data-remote= "true" > Show </a>

Adopting Unobtrusive Javascript

Rails 2

Rails 3

Trang 73

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

<%= form_for ( @post , :remote => true ) 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;" >

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

Adopting Unobtrusive Javascript

Rails 2

Rails 3

Trang 74

<%= link_to 'Destroy' , post, :method => :delete %>

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

<a href="/posts/1" onclick="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>

Adopting Unobtrusive Javascript

Rails 2 Rails 3

Trang 75

<%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>

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

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

Adopting Unobtrusive Javascript

Rails 2 Rails 3

Trang 76

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

Adopting Unobtrusive Javascript

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

<input data-disable-with= "Please wait "

id= "post_submit" name= "commit" type= "submit" value= "Create Post" />

Rails 2

Rails 3

Trang 78

document observe( "dom:loaded" , function () {

$ ( document body ).observe( "click" , function ( event ) {

Trang 81

<meta name="csrf-param" content="authenticity_token"/>

<meta name="csrf-token" content="I+d jI="/>

<%= csrf_meta_tag %> (in your layout)

rails.js unobtrusively adds the token

Trang 82

XSS & UJS

Tutorial - Lab #5

Trang 83

Getting Started & Routes

Trang 85

name author URL rainbow of 80s toys merwing✿little dear http://www.flickr.com/photos/merwing/2152164258/

Notting Hill Gate Eole http://www.flickr.com/photos/eole/942309733/

Das Licht Small http://www.flickr.com/photos/small/62713023/

Metro Genova opti mystic http://www.flickr.com/photos/miiilio/2503634282/

Immobility Dilemna gilderic http://www.flickr.com/photos/gilderic/3528157964/

train station nolifebeforecoffee http://www.flickr.com/photos/nolifebeforecoffee/1803584805/

Mystical station Jsome1 http://www.flickr.com/photos/jsome1/2226394415/

Railswaystation Pieter Musterd http://www.flickr.com/photos/piet_musterd/2233025691/

The Handover MarkyBon http://www.flickr.com/photos/markybon/152769885/

EN57 magro_kr http://www.flickr.com/photos/iks_berto/1328682171/

Adirondack Extreme Mikey Roach http://www.flickr.com/photos/mikeroach/4576888456/

IMG_1242 khoogheem http://www.flickr.com/photos/khoogheem/3534078991/

Creative Commons

Trang 86

If you need help with a Rails 3

project, feel free to give us a call

Ngày đăng: 16/03/2014, 11:20

w