Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ruby on Rails -- a short intro Tao Zhang. Outline Part 1: sth. about script language Part 2: sth. about RoR.

Similar presentations


Presentation on theme: "Ruby on Rails -- a short intro Tao Zhang. Outline Part 1: sth. about script language Part 2: sth. about RoR."— Presentation transcript:

1 Ruby on Rails -- a short intro Tao Zhang

2 Outline Part 1: sth. about script language Part 2: sth. about RoR

3 Part 1: script language

4 Hash { } Ruby  array= []  hash= {} Javascript:  var array= [];  var obj={}; Ruby  array= [‘a’, 256, var1]  hash= {:symbol=>256, :key=>’value’, ‘anything’=>array}  hash[:key]= ‘value’ Javascript:  var array= [‘a’, 256, var1];  var obj={key: ‘value’, key2: 256, key3: obj};  hash.key= ‘value’

5 JSON (array & hash) var json_obj1= { key: [‘a’,256, var], key2: ‘string’ }; var json_obj2= [ ‘a’, 256, {key:’value’, key2:’value2’}, [‘a’,256, var] ];

6 Objects in javascript Javascript 没有类 (class), 只有对象 (object) 两种对象  JSON: 字面量 (literal) 对象 / 一次性 (one-off) 对象  Function: 基于原型 (prototype) 的, 可复制的, 可动态修改的对象 用关键字 new 复制 function 对象

7 Function and parameters Java :  javaFunction(int param1, char param2, double param3, ClassName param4)  {  // definition  }  //call it  javaFunction(256, ‘a’, 23.233, object);  //or  javaFunction(null, ‘a’, null, object);

8 Function and parameters Ruby :  def method(:opt=>{}) # definition  end  #call it  method( {:title=>‘a’, :width=>200, :height=>300})  #or  method(:title=>‘a’, :width=>200, :height=>300)  #or  method :title=>‘a’, :width=>200, :height=>300

9 block def one_block  yield end one_block { puts "This is a block. " } Output: This is a block.

10 block def one_block  for num in 1..3 yield(num)  end end one_block do |i|  puts "This is block #{i}. " end Output: This is block 1. This is block 2. This is block 3.

11 block (real example) class SamplesController < ApplicationController # GET /samples # GET /samples.xml def index @samples = Sample.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @samples } end 1 2 3

12 block (real example) #config/routes.rb: ActionController::Routing::Routes.draw do |map|  map.resources :files, :only => :show  map.connect ':controller/:action/:id’  map.connect ':controller/:action/:id.:format' end #vendor/rails/actionpack/lib/action_controller/routing/route_set.rb def draw  yield Mapper.new(self)  install_helpers end

13 Part 2: RoR

14 Directory Structure app  Controllers  Models  Views config  Database.yml  Routes.rb db  Migrate/  Schema.rb public script vendor Active Record ActionPack 互相独立

15 Rails 工作原理 routes.rb 是 url 解析和生成的配置文件 ( 规则 ) 一般规则 : domain.com/controller/action/id #app/controllers/samples_controller.rb class SamplesController < ApplicationController def action # anything you want to do render #render template, creating view(html pages, xml, javascripts, …) end def another_action end

16 定义 route 与解析 url rake routes # Print out all defined routes in match order, with names. map.connect ‘whateverstring/whatever’, :controller => ‘xxx’, :action =>’yyy’ domain.com/whateverstring/whatever 就会调用 xxx.yyy 方法

17 生成 url A helper method called “url_for”: url_for :controller =>’xxx’, :action =>’yyy’ 的返回值为 ‘domain.com/whateverstring/whatever’ Named route: map.wuyan ‘whateverstring/whatever’, :controller => ‘xxx’, :action =>’yyy’ Rails 会为你自动生成两个 helper method:  wuyan_url# 返回值为 ‘domain.com/whateverstring/whatever’  wuyan_path# 返回值为 ‘whateverstring/whatever’ 举例 : SamplesController.destroy()

18 REST & resource-based routing 只是预先提供的一套特殊形式的 routes map.resource :samples 将自动为你生成 7 个 named routes, 包括了基本的 CRUD 动作 举例 : rake routes views/samples/index.html.erb

19 Action View & template 各种 helper method, 用于动态生成网页 /xml/javascripts Template: 指.html.erb 文件,.builder 文件,.rjs 文件 比如 :  link_to, button_to, form_for

20 render render, redirect_to, send_data, send_file 这几个方法将 respond to a request, 即最终返回数据给 web browser app/controllers/wuyan_controller.rb 中的各个 action 对应与 app/views/wuyan/action*.html.erb  比如 :  #wuyan_controller.rb:  class WuyanController < ApplicationController def show  render :template=>’whatever’ end  end  那么 show 方法会将 app/views/wuyan/whatever.html.erb 页面返回


Download ppt "Ruby on Rails -- a short intro Tao Zhang. Outline Part 1: sth. about script language Part 2: sth. about RoR."

Similar presentations


Ads by Google