Shallow Routes

Rails was designed to help reduce the complexity of routing by introducing a built-in shallow routing option

Configuring Routes

Here’s an example of how shallow routing would work by design according to the Rails Guide.

resources :articles do
  resources :comments, only: [:index, :new, :create]
resources :comments, only: [:show, :edit, :update, :destroy]

There is a short hand for this in Rails and it looks like this:

resources :articles, shallow: true do
  resources :comments
  resources :quotes
  resources :drafts

This results in all of the sub-resources to be shallow based on this configuration

Forms configuration

My initial reaction for leveraging the shallow routing was to limit all interactions with the form to the sub-resource and all the view only actions to route at the parent level. However, Rails has figured all of this out for us already. Check out this link on StackOverflow for details.

The general gist is that the first object in the array you pass the form builder is nil, Rails will POST to the second object only. In the example above, you wouldn’t set the @article object in your comments controller edit action. If you need access to the article object, you would access it through @comment

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.