Learning Rails Routing

OK, don’t laugh at me, I’m still learning. I have read the documentation on rails routing on the rails guide website. I’m working on a simple blog and want the routes to look like so:

mywebsite.com/category-name

mywebsite.com/category-name/article-name

The resources are article_categories and articles.

Right now, I can easily get this url structure with this route setup:

  resources :article_categories, only: [:show], path: '' do
    resources :articles, only: [:show], path: ''
  end  resources :article_categories, only: [:show], path: '' do
    resources :articles, only: [:show], path: ''
  end

However, it must live at the bottom of all routes. I completely understand why that is and how it works. But it seems this means you can only do one resource this way because any others you put below that will not work correctly (maybe I’m wrong about this…).

So my quest is for understanding on how I can create these routes so that they work even if they aren’t at the bottom of the routes file and at the same time, keep the same url helper names the other code provides.

I have tried some direct custom routes like “get “/:id”, as: :article_category …” and they worked but still had the same issue of needing to be at the bottom of the file.

I saw something about using constraints and what not but I’m not sure if that really applies. If what I’m looking for isn’t possible and the only option I have is to put the routes at the bottom for only one resource, Ok. But I thought this must be a gap in my understanding of rails routes.

Thank you.

I have done this kind of “wildcard” routing as well in some of my apps, but the fact that the routes are parsed in order means that if you wildcard the first segment, then no remaining routes will ever “win”, no matter how specifically they match. That’s why such a wildcard has to appear last in the routes file. I usually just put a comment in there to that effect and carry on. I have no idea if there is any way around this, but in the past I never found one.

Walter

(post deleted by author)

Thanks for your reply. I was thinking this might be the reality but thought I’d be in denial about it for a bit…. and I am new so thought maybe it was just my lack of understanding. Maybe if I created custom routes for the entire app this wouldn’t be the issue… I don’t know. I’ll leave it at the bottom then but it saddens me that I can only do this for one set of related resources.

No sooner had I typed the above, when this article popped into my feed reader: The SaaS Architecture Guide: How to Handle Multitenancy in Rails Routing - DevBlog by Zil Norvilis and there’s some bits in here around the constraints options in the router that you might want to explore. The idea is that you could check (in your wildcard route) if the first segment matched any of your existing controller names, and reject that path as matching your wildcard route. Now you will need to be clear in your system that an article could never ever have a slug that matches one of your other controllers. But that would be true anyway…

1 Like

Thank you! I will read over the article. I tried some constraints given by AI and they didn’t work but I find sometimes, there is a lack of understanding there. So I don’t use it anymore and prefer to find some text I can walk through myself and get it figured out.

1 Like