【问题标题】:RESTful routes in Padrino controllersPadrino 控制器中的 RESTful 路由
【发布时间】:2014-08-12 18:57:30
【问题描述】:

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

这可能吗?

【问题讨论】:

    标签: sinatra controllers padrino


    【解决方案1】:
    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
    

    【讨论】:

    • 如果用户不在场怎么办? /tweets/tweets/:id 还能用吗?
    • 这取决于您如何设置控制器内部的逻辑。您指的是哪条路线?
    • 假装没有逻辑,我们只是返回一个字符串。 “GET /tweets”和“GET /users/:user_id/tweets”会出现在同一个地方吗?
    • 然后尝试在这些路由中调用其他操作:调用 env.merge("PATH_INFO" => url(:users, :tweets, :user_id => params[:id])) 更多here -> stackoverflow.com/questions/3384134/…
    【解决方案2】:

    您是否在路线定义(第一行)中尝试过 :optional => true ?

    【讨论】:

      猜你喜欢
      • 2014-01-24
      • 2012-12-24
      • 2016-08-08
      • 2013-06-09
      • 1970-01-01
      • 2013-06-04
      • 2015-02-14
      • 2012-11-05
      • 2011-12-02
      相关资源
      最近更新 更多