Skip to content

Instantly share code, notes, and snippets.

@ashleygwilliams
Last active December 19, 2015 14:28
Show Gist options
  • Save ashleygwilliams/5969011 to your computer and use it in GitHub Desktop.
Save ashleygwilliams/5969011 to your computer and use it in GitHub Desktop.

Shallow Routes: Best Practices

FROM: http://edgeguides.rubyonrails.org/routing.html

TAKEAWAY: you want routes that are DESCRIPTIVE, but you also want routes that are not stupid deep. therefore, make shallow routes. there is an shallow option for this.

  1. only build routes with the minimal amount of information to uniquely identify the resource, like this:
resources :posts do
  resources :comments, only: [:index, :new, :create]
end
resources :comments, only: [:show, :edit, :update, :destroy]
  1. RAILS AUTOMAGIC

A. use the shallow option on any resource to make it shallow

resources :posts do
  resources :comments, shallow: true
end

B. or do it on the parent resource to make everything shallow

resources :posts, shallow: true do
  resources :comments
  resources :quotes
  resources :drafts
end

C. you could also do this like this. this is the same as B.

shallow do
  resources :posts do
    resources :comments
    resources :quotes
    resources :drafts
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment