0

Padrino supports the idea of nested routes. Here's one example from the documentation:

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

However, what I'd like is to be able to have the following mappings:

 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

Is that possible?

John Feminella
  • 303,634
  • 46
  • 339
  • 357

2 Answers2

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
lmerino
  • 173
  • 2
  • 11
  • What if a user's not present? Does `/tweets` and `/tweets/:id` still work? – John Feminella Jul 29 '14 at 08:18
  • That depends on how you set-up the logic inside your controllers. Which route would you be referring to? – lmerino Jul 29 '14 at 12:42
  • Pretend there's no logic and that we just return a string. Will "GET /tweets" and "GET /users/:user_id/tweets" wind up at the same spot? – John Feminella Jul 29 '14 at 15:44
  • Then try to call the other actions within those routes: call env.merge("PATH_INFO" => url(:users, :tweets, :user_id => params[:id])) more here -> http://stackoverflow.com/questions/3384134/alias-url-with-sinatra-padrino – lmerino Jul 29 '14 at 15:47
0

Have you tried :optional => true in your route defininition (1st line)?

lmerino
  • 173
  • 2
  • 11