I have a table called Metrics that is a self-referencing table. Meaning it has a parent/child relationship with infinite children.
I have a route like:
match "/metrics/:id/children" => "metricchildren#index", :as => "metric_children"
The metricchildren contoller loads a metric’s children and renders an index page.
That page looks something like (root parent called Totals):
Metric Children?
----------------------------
Total Expense YES
Total Labor NO
Now, what I want to do is have the user click the Total Expense and then the URL would look something like:
http://example.com/metrics/42-totals/children/78-total-expense
42 is the ID of the Totals metric, 78 is the ID of the Total Expense metric. So when you drill down to Total Expense it would create a table as above and as you click the metrics, the urls grow like:
http://example.com/metrics/42-totals/children/78-total-expense/98-total-direct
http://example.com/metrics/42-totals/children/78-total-expense/98-total-direct/102-blah
etc…
Very similar to how Github works. Even better would be the ability to not have to specify the ID’s in the URL.
Any gems or ideas on how to do this? I think I have an idea on how to do it but I don’t want to reinvent that wheel.
Thanks
As for the “recursive routes”, see this other SO post. Short answer is, it’s actually pretty easy with route globbing.
Now, I roll my own pretty permalinks, using a slug instead of the database ID, but I’ve heard great things about friendly_id.