Ruby on Rails Intro

890 views

Published on

introducing the essence of how rails works, also talk some basic common concepts in script languages

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
890
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ruby on Rails Intro

  1. 1. Ruby on Rails -- a short intro Tao Zhang
  2. 2. Outline <ul><li>Part 1: sth. about script language </li></ul><ul><li>Part 2: sth. about RoR </li></ul>
  3. 3. Part 1: script language
  4. 4. Hash { } <ul><li>Ruby </li></ul><ul><ul><li>array= [] </li></ul></ul><ul><ul><li>hash= {} </li></ul></ul><ul><li>Javascript: </li></ul><ul><ul><li>var array= []; </li></ul></ul><ul><ul><li>var obj={}; </li></ul></ul><ul><li>Ruby </li></ul><ul><ul><li>array= [‘a’, 256, var1] </li></ul></ul><ul><ul><li>hash= {:symbol=>256, :key=>’value’, ‘anything’=>array} </li></ul></ul><ul><ul><li>hash[:key]= ‘value’ </li></ul></ul><ul><li>Javascript: </li></ul><ul><ul><li>var array= [‘a’, 256, var1]; </li></ul></ul><ul><ul><li>var obj={key: ‘value’, key2: 256, key3: obj}; </li></ul></ul><ul><ul><li>hash.key= ‘value’ </li></ul></ul>
  5. 5. JSON (array & hash) <ul><li>var json_obj1= { key: [‘a’,256, var], </li></ul><ul><li>key2: ‘string’ }; </li></ul><ul><li>var json_obj2= [ ‘a’, 256, </li></ul><ul><li>{key:’value’, key2:’value2’}, </li></ul><ul><li>[‘a’,256, var] ]; </li></ul>
  6. 6. Objects in javascript <ul><li>Javascript 没有类 (class), 只有对象 (object) </li></ul><ul><li>两种对象 </li></ul><ul><ul><li>JSON: </li></ul></ul><ul><ul><ul><li>字面量 (literal) 对象 / 一次性 (one-off) 对象 </li></ul></ul></ul><ul><ul><li>Function: </li></ul></ul><ul><ul><ul><li>基于原型 (prototype) 的 , 可复制的 , 可动态修改的对象 </li></ul></ul></ul><ul><ul><ul><li>用关键字 new 复制 function 对象 </li></ul></ul></ul>
  7. 7. Function and parameters <ul><li>Java : </li></ul><ul><ul><li>javaFunction(int param1, char param2, double param3, ClassName param4) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>// definition </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>//call it </li></ul></ul><ul><ul><li>javaFunction(256, ‘a’, 23.233, object); </li></ul></ul><ul><ul><li>//or </li></ul></ul><ul><ul><li>javaFunction(null, ‘a’, null, object); </li></ul></ul>
  8. 8. Function and parameters <ul><li>Ruby : </li></ul><ul><ul><li>def method(:opt=>{}) </li></ul></ul><ul><ul><ul><li># definition </li></ul></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>#call it </li></ul></ul><ul><ul><li>method( {:title=>‘a’, :width=>200, :height=>300}) </li></ul></ul><ul><ul><li>#or </li></ul></ul><ul><ul><li>method(:title=>‘a’, :width=>200, :height=>300) </li></ul></ul><ul><ul><li>#or </li></ul></ul><ul><ul><li>method :title=>‘a’, :width=>200, :height=>300 </li></ul></ul>
  9. 9. block <ul><li>def one_block </li></ul><ul><ul><li>yield </li></ul></ul><ul><ul><li>yield </li></ul></ul><ul><ul><li>yield </li></ul></ul><ul><li>end </li></ul><ul><li>one_block { puts &quot;This is a block. &quot; } </li></ul><ul><li>Output: </li></ul><ul><li>This is a block. </li></ul><ul><li>This is a block. </li></ul><ul><li>This is a block. </li></ul>
  10. 10. block <ul><li>def one_block </li></ul><ul><ul><li>for num in 1..3 </li></ul></ul><ul><ul><ul><li>yield(num) </li></ul></ul></ul><ul><ul><li>end </li></ul></ul><ul><li>end </li></ul><ul><li>one_block do |i| </li></ul><ul><ul><li>puts &quot;This is block #{i}. &quot; </li></ul></ul><ul><li>end </li></ul><ul><li>Output: </li></ul><ul><li>This is block 1. This is block 2. This is block 3. </li></ul>
  11. 11. block (real example) <ul><li>class SamplesController < ApplicationController </li></ul><ul><li># GET /samples </li></ul><ul><li># GET /samples.xml </li></ul><ul><li>def index </li></ul><ul><li>@samples = Sample.all </li></ul><ul><li>respond_to do |format| </li></ul><ul><li>format.html # index.html.erb </li></ul><ul><li>format.xml { render :xml => @samples } </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>1 2 3
  12. 12. block (real example) <ul><li>#config/routes.rb: </li></ul><ul><li>ActionController::Routing::Routes.draw do |map| </li></ul><ul><ul><li>map.resources :files, :only => :show </li></ul></ul><ul><ul><li>map.connect ':controller/:action/:id’ </li></ul></ul><ul><ul><li>map.connect ':controller/:action/:id.:format' </li></ul></ul><ul><li>end </li></ul><ul><li>#vendor/rails/actionpack/lib/action_controller/routing/route_set.rb </li></ul><ul><li>def draw </li></ul><ul><ul><li>yield Mapper.new( self ) </li></ul></ul><ul><ul><li>install_helpers </li></ul></ul><ul><li>end </li></ul>
  13. 13. Part 2: RoR
  14. 14. Directory Structure <ul><li>app </li></ul><ul><ul><li>Controllers </li></ul></ul><ul><ul><li>Models </li></ul></ul><ul><ul><li>Views </li></ul></ul><ul><li>config </li></ul><ul><ul><li>Database.yml </li></ul></ul><ul><ul><li>Routes.rb </li></ul></ul><ul><li>db </li></ul><ul><ul><li>Migrate/ </li></ul></ul><ul><ul><li>Schema.rb </li></ul></ul><ul><li>public </li></ul><ul><li>script </li></ul><ul><li>vendor </li></ul>Active Record ActionPack 互相独立
  15. 15. R ails 工作原理 <ul><li>routes.rb 是 url 解析和生成的配置文件 ( 规则 ) </li></ul><ul><li>一般规则 : </li></ul><ul><li>domain.com/controller/action/id </li></ul>#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 end
  16. 16. 定义 route 与解析 url <ul><li>rake routes # Print out all defined routes in match order, with names. </li></ul><ul><li>map.connect ‘whateverstring/whatever’, </li></ul><ul><li>:controller => ‘xxx’, </li></ul><ul><li>:action =>’yyy’ </li></ul><ul><li>domain.com/whateverstring/whatever 就会调用 xxx.yyy 方法 </li></ul>
  17. 17. 生成 url <ul><li>A helper method called “url_for”: </li></ul><ul><li>url_for :controller =>’xxx’, :action =>’yyy’ </li></ul><ul><li>的返回值为 ‘ domain.com/whateverstring/whatever ’ </li></ul><ul><li>Named route: </li></ul><ul><li>map.wuyan ‘whateverstring/whatever’, </li></ul><ul><li>:controller => ‘xxx’, </li></ul><ul><li>:action =>’yyy’ </li></ul><ul><li>R ails 会为你自动生成两个 helper method: </li></ul><ul><ul><li>wuyan_url # 返回值为 ‘ domain.com/whateverstring/whatever ’ </li></ul></ul><ul><ul><li>wuyan_path # 返回值为 ‘ whateverstring/whatever’ </li></ul></ul>举例 : SamplesController.destroy()
  18. 18. REST & resource-based routing <ul><li>只是预先提供的一套特殊形式的 routes </li></ul><ul><li>map.resource :samples 将自动为你生成 7 个 named routes, 包括了基本的 CRUD 动作 </li></ul>举例 : rake routes views/samples/index.html.erb
  19. 19. Action View & template <ul><li>各种 helper method, 用于动态生成网页 /xml/javascripts </li></ul><ul><li>Template: 指 .html.erb 文件 , .builder 文件 , .rjs 文件 </li></ul><ul><li>比如 : </li></ul><ul><ul><li>link_to, button_to, form_for </li></ul></ul>
  20. 20. render <ul><li>render, redirect_to, send_data, send_file </li></ul><ul><li>这几个方法将 respond to a request, 即最终返回数据给 web browser </li></ul><ul><li>app/controllers/wuyan_controller.rb 中的各个 action </li></ul><ul><li>对应与 app/views/wuyan/action * .html.erb </li></ul><ul><ul><li>比如 : </li></ul></ul><ul><ul><li>#wuyan_controller.rb: </li></ul></ul><ul><ul><li>class WuyanController < ApplicationController </li></ul></ul><ul><ul><ul><li>def show </li></ul></ul></ul><ul><ul><ul><ul><li>render :template=>’whatever’ </li></ul></ul></ul></ul><ul><ul><ul><li>end </li></ul></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>那么 show 方法会将 app/views/wuyan/whatever.html.erb 页面返回 </li></ul></ul>

×