rails 的 REST 结构和 route 代码分析

最近做ruby rails 项目的开发, 因为以前做j2ee开发,所以当接触rails的时候,感慨良多, 开始有点喜欢ruby on rails。
其它的优点先不说了,简单聊聊rails的REST结构。

 rails 的REST 的结构来源于HTTP协议的设计,HTTP协议中主要有GET、POST、PUT、DELETE 方法,当初设计的思路是按照resource的CRUD(增,删,改,查)操作,每次发起一个http请求,就意味着对服务器端资源的增加,或者删除,或者修改,或者查询。
例如:
Get /products/1  # 对编号为1的product进行查询
Post /products   #创建一个新的product

虽然HTTP是这样的设计初衷,但是在应用级别上,并没有强制上述那种映射关系。
如: java 的servlet, 即使提交时用的GET方法,仍然可以删除,或创建任何数据。

REST架构继承了HTTP的设计思路。
在rails中,默认的情况下,有如下的最基本的对应关系:
method => action (in controller)
GET => show
POST => update
PUT => create
DELETE => delete

rails是如何通过url找到对应的action的呢?
下面举一个例子来说明 route.rb是如何来编写路由控制的
#################################################################
ActionController::Routing::Routes.draw do |map|
  # The priority is based upon order of creation: first created -> highest priority.

  map.resources :products, :member => { :ma => :get, :mb => :post, :mc => :put, :md => :delete },
    :collection => { :a => :get, :b => :post, :c => :put, :d => :delete }  do |products|
    products.products_made_in_country  ":action/:country_id", :controller => :products, :action => :products_by_country
    products.resources "vendors", :collection => {:list => :get}
  end

  # default route rules.
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end
###################################################################
map.resources :products 意思是, 在根目录下,有一项资源是products,即一些products
:member => { :ma => :get, :mb => :post, :mc => :put, :md => :delete } 表示, 请求products里的每一个成员的时候,如果HTTP 方法
是GET,并且对应的action是ma的话,rails 就会去找products controller 里面的ma action。

resource code                        URL                                    action
map.resources                       GET /products                   index
map.resources                       GET /products/a                 a
map.resources                       POST /products                  update
map.resources                       POST /products/b               b
collection => { :a => :get}    GET /products/a                 a
map.resources                       GET /products/3213         show
:member => { :ma => :get)   GET/products/3213/ma       ma

product.resource "vendors"意思是在products级别下还有叫vendors的resource。
这样resource就实现了嵌套

So , GET /products/123/vendors/list 就会对应到vemdor的controller中的list action。

collection 表示对于集合的操作;
member 表示对于集合中每一项资源的操作;
new 表示在创建一个资源成员的时候,对应的操.
如:
  map.resources :messages, :path_prefix => "/thread/:thread_id"
# --> GET /thread/7/messages/1

map.resources :messages, :collection => { :rss => :get }
# --> GET /messages/rss (maps to the #rss action)
# also adds a named route called "rss_messages"

map.resources :messages, :member => { :mark => :post }
# --> POST /messages/1/mark (maps to the #mark action)
# also adds a named route called "mark_message"

map.resources :messages, :new => { :preview => :post }
# --> POST /messages/new/preview (maps to the #preview action)
# also adds a named route called "preview_new_message"

map.resources :messages, :new => { :new => :any, :preview => :post }
# --> POST /messages/new/preview (maps to the #preview action)
# also adds a named route called "preview_new_message"
# --> /messages/new can be invoked via any request method

map.resources :messages, :controller => "categories",
:path_prefix => "/category/:category_id",
:name_prefix => "category_"
# --> GET /categories/7/messages/1
# has named route "category_message"


to be comtinued ..................

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值