六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 95|回复: 0

Resource-Based Routing

[复制链接]

升级  14.67%

16

主题

16

主题

16

主题

秀才

Rank: 2

积分
72
 楼主| 发表于 2013-2-7 19:28:28 | 显示全部楼层 |阅读模式
基本增加的内容

Resource-Based Routing依靠restful的风格,定义后增加下面4个helper
 
  client_url       show     update    destroy
  clients_url      index    create
  edit_client_url  edit
  new_client_url   new

  update和destroy通过传入:method来完成
    对于link_to form_tag可直接在client_url后面跟着:method => :delete
    对于form_for来说,会要求跟在一个嵌套hash中 form_for @client, :html => {:method=>:delete}
 
 
单复数route
  两种使用helper的形式
    client_url(@item)        ----参数形式
    clent_url(:id => @item)  ----hash形式

定义自己的action

map.resources :articles, :collection => { :recent => :get }
:collection增加了recent action包括
Method: get
path: /articles/recent
action: recent
helper: recent_articles_url

:member则给某个实例增加action
map.resources :articles, :member => {:release => :put, :rollback => :put}
Method: put
path: /articles/1/release
action: release
helper: release_article_url
Nested Resources

 有些资源有依赖关系,需要成对出现
  嵌套定音的后果
    map.resources :auctions do |auction|
      auction.resources :bids
    end
      除了autions的4个helper,也为bids定义了auction_bids_url, new_auction_bid_url等等helper。
      nested 使得任何使用bid routes的时候,必须提供auction资源
      对于singular routes需要提供至少两个参数
    
    :name_perfix 使得可以定义helper的前缀
      用nil表示取消前缀
   对于form_for可以采用
     <% form_for([:admin, @post]) do |f| %>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表