Padrino控制器中的RESTful路由

时间:2014-06-03 17:47:56

标签: sinatra controllers padrino

Padrino支持嵌套路由的想法。以下是文档中的一个示例:

SimpleApp.controllers :product, :parent => :user do
  get :index do
    # "/user/#{params[:user_id]}/product"
  end

  get :show, :with => :id do
    # "/user/#{params[:user_id]}/product/show/#{params[:id]}"
  end
end

但是,我想要的是能够拥有以下映射:

 GET /users                       # '/' in :users controller
 GET /users/:id                   # '/:id' in :users controller

 GET /users/:user_id/tweets       # '/' in :tweets controller
 GET /users/:user_id/tweets/:id   # '/:id' in :tweets controller

 GET /tweets                      # '/' in :tweets controller, too
 GET /tweets/:id                  # '/:id' in :tweets controller, too

这可能吗?

2 个答案:

答案 0 :(得分:0)

SimpleApp.controllers :tweets, :parent => :user do
  get :index do
    # "/user/#{params[:user_id]}/tweets"
  end

  get :index, :with => :id do
    # "/user/#{params[:user_id]}/tweets/#{params[:id]}"
  end
end

答案 1 :(得分:0)

您是否尝试过:optional =>您的路线定义(第1行)是真的吗?