It seems like this is possible on the Rails side, having the entire route for // slug, and then using slug for the first request for the user, and then the request for the organization.
This is the only viable approach. You need to add this generic route at the very end of your config / routes.rb file. I advise you to associate this route with the proxy method in your controller, which will determine if the User or Organization slug entry matches.
Knowing that you must take into account the application when User and Organization have the same slug (some will prioritize one of them, some of them will provide the visitor with 2 options); and the action to be taken when the entry does not match slug (some of them will display 404, and some will go to the list page). These options will help you determine the best design for your proxy method.
The main thing here is to create this proxy method.
1- HTTP redirection
Your proxy method will respond redirect_to to / users /: slug or / organization /: slug depending on what is found in slug .
- Advantage: easy and fast to write and maintain.
- Disadvantage: slow application response due to HTTP redirection and the need to re-access the database during the second request.
2-Ajax answer
Similar to HTTP redirect, but performed in AJAX. Your proxy method will display a page containing JavaScript code that extracts the contents of / users /: slug or / organization /: slug and displays it through AJAX.
Alternatively, the URL can be changed using JavaScript using history.pushState
- Advantage: a little faster than higher.
- Disadvantage: but itβs more difficult to write, you need JavaScript code plus a js representation, and you still need to re-access the database during the second request.
3-server proxy
Your proxy method will execute the same code as your endpoint method. This means that if the slug found matches the User entry, the proxy method will do the same as UserController#show . (And the same goes for Organization )
This means that the proxy method should call all the filters before / after that UserController#show will be, and display the same view.
Be very careful if you choose this solution for DRY. Or you may have difficulty maintaining your application.
There is not a single method for this, and it all depends on the architecture of your application. If, for example, your show method simply retrieves the record and renders the view, and if there are no controller methods, then this is easy and safe. On the other hand, if you have filters and methods for controller assistants, then this gets a little more complicated. In this case, you can rely on problems with the controller.
- Benefit: Quick Solution
- Disadvantage: it can be very difficult to write, and depending on the evolution of your code base in the future, you may find yourself in a blocking situation when you have to abandon this development.