Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3, shallow routes

In rails 2.x I used shallow routes, but this seems to be missing from rails 3 (at least in the API http://apidock.com/rails/ActionController/Resources/resources).

When I pass this option in rails 3 it doesn't throw any errors, but I'm also not getting all of the routes I expected.

Rails 3 routes.rb

  resources :users, :shallow=>true do
    resources :recipe do
      resources :categories do
        resources :sections do
          resources :details do
          end
        end
      end
    end
  end

The routes missing that were generated with the rails 2.x equivalent are (just a sample for the recipe resource):

GET new_recipe (I only have new_user_recipe), and

POST recipe (to create a new recipe, I only have POST user_recipe)

It kind of makes sense that these routes wouldn't be generated, but my old code worked around it by passing the user_id in each form (less elegant, agreed).

Question is: Is there documentation for 'shallow' routes in rails 3? Is there a way to generate the routes I'm missing from rails 2.x?

Thanks, Mike

like image 640
CambridgeMike Avatar asked Apr 08 '11 23:04

CambridgeMike


1 Answers

You need to apply the :shallow option to the nested resources. This should give you what you want:

  resources :users do
    resources :recipe, :shallow=>true do
      resources :categories do
        resources :sections do
          resources :details do
          end
        end
      end
    end
  end  
like image 109
opsb Avatar answered Oct 19 '22 05:10

opsb