Most of the time, I’m trying to write the most conventional, boring Rails code possible. If I’m spending time remembering/looking up rarely-used parameters and sweet tricks, I may have gone down a not-invented-here rabbit hole and needed to bounce back out.

But every once in a while, it’s reasonable to experiment with infrequently-used parameters to routes or associations to get things just right for the future. In that case, I’m often tinkering with some bit of routing configuration like this:

  resources :users do
    resources :posts, shallow: true, only: %i[show create] do
      collection do
        post :reactivate, as: :restore
      end
    end
  end

The trick is to remember the parameter name for shallow or to get the path and parameters and helper name for the reactivate action just so.

When I’m expressing my artistry via Rails routes like this, I use a few tools:

  • frequent, iterative use of rails routes|grep users so I can see all the routes I’m generating and if the parameter I’m using has the result I want
  • lots of reading in the Rails routing guide; excellent docs here

If iterating from the command-line isn’t your thing, /rails/info/routes tells much the same story and lets you filter the route list down to a manageable subset in much the same ways as grep does.