0

I want to group routes into hierarchies. To that end I have created a top level controller thus:

MyApp.controllers :group do
  disable :layout

  get '/' do
    {'dummy' => 'value'}.as_json
  end
end

Now I want to create routes with :group as parent thus:

MyApp.controllers :items, :parent => :group do
  disable :layout

  get '/', :provides => :json do
    # get list of items
  end
end

My problem is that I can access localhost:port/group (200 OK) from my REST client but I cannot access localhost:port/group/items (404 NOT FOUND).

Everything works beautifully if I remove the :parent option. My padrino version 0.10.5.5. Any ideas?

341008
  • 9,862
  • 11
  • 52
  • 84

1 Answers1

3

The way nested routes work is different than what you're trying to do.

When you use :parent, it expects the parent resource to be identified somehow. For instance:

MyApp.controllers :items, :parent => :group do
  get :index do
    # returns list of items for a group
  end
end

Will generate the following route:

"/group/#{params[:group_id]}/items"

It expects you to specify which group you're talking about in order to fetch the items for that specific group.

That's why it says it cannot find "/group/items", it's not the route that you create by using :parent => :group, you're not passing a :group_id in the params.

If you really want to use "/group/items" then your items controller should look like:

MyApp.controllers :items do
  get '/group/items' do
    # returns list of items
  end
end
Marc Lainez
  • 3,070
  • 11
  • 16