The Nuances of Nested Routing
Order matters when it comes to rails forms and nested routes
Now that I’m nearly three months into a fairly intensive coding boot camp program, I’ve come to realize that the most impactful learning experiences in programming usually follow those long periods of confusion, anger, and ultimately, crippling depression following an “unsolvable” problem.
My latest project presented just such a scenario. And considering how simple the fix ended up being, it was easy to get caught up in the above three emotions once I realized I’d just wasted four hours on a single line of code that should have been glaringly obvious.
Instead, I chose to look at the painful ordeal as a learning opportunity, because nothing hammers a concept into your head like an entire day wasted— a journey most people like to avoid for a second time.
My particular issue happened to deal with nested routes. Unlike our work with Sinatra, this concept reared its head once we learned the fundamentals of Rails.
My project has three main models that make up the bulk of the user input and database manipulations: 1) A User model that has many subscriptions, and has many Streaming Services through subscriptions. 2) A Subscriptions model that belongs to both a User model and a Streaming Services model. 3) A Streaming Service model that has many Subscriptions and has many Users through Subscriptions.
Every time I created a subscription, that subscription was nested within a streaming_service/:id/subscription/:id in the URL. I then pulled that streaming service id param and passed it on as a hidden value into the subscriptions controller to manually assign a streaming service to each subscription. Pretty basic, right? It was. Until things came crashing down.
The problem was that when I went to edit said subscriptions, the id value of a subscription’s streaming service was being swapped with the subscription’s own id value, causing all sorts of trouble.
After several hours of checking params with the pry gem and combing through my view forms, I realized I had an unmistakable flaw in my code. Can you spot it?
When it comes to Rails forms, order matters — especially when dealing with nested routes. My link_to path above passed in parameters of (subscription, streaming_services), which is completely wrong, because a subscription should be nested within a streaming service, the object it belongs to, not the other way around.
This is how the link_to parameters should look, nested according to the relationships established in the model files.
This took me way too long to figure out, and was a great example of a very simple fix that could potentially ruin your day while the same time teaching you a lesson you’ll never forget.
So don’t get discouraged when you hit a roadblock like this. For each hour you spend frantically looking for the source of your problem, the chances you’ll never make that mistake again increase exponentially!
Of course I’m being facetious, but there is a lot of truth to this concept.
Instead of spiraling into despair and frustration upon hitting a snag, acknowledge that you will absolutely be a better programmer on the other side of your never-ending mission to diagnose your application’s dysfunction. Take it from the king of missing stupid mistakes for hours on end. There is a light at the end of the tunnel. Stay positive and keep coding.