I have a Consultant model, where i have multiple consultant types (lawyers, doctors, psychologists etc.) listed on different websites, all handled from the same rails project.
I would like to have the consultant type as a part of the url, but have a hard time figuring out how, since it is dynamic based on domain/consultant type.
I am hoping for a solution to do a standard link:
=link_to consultant.name, consultant
without any specific link-config, so I can re-use templates across multiple consultant-websites.
Urls should be like this:
a-domain.com/doctor/doctor-name
b-domain.com/lawyer/lawyer-name
What I've tried so far, and used in the domain-specific templates (i know it is an ugly solution):
routes.rb
get 'lawyer/:slug' => 'consultants#show', as: :lawyer_consultant
get 'doctor/:slug' => 'consultants#show', as: :doctor_consultant
_consultant.html.haml for a-domain.com
= link_to consultant.name, lawyer_consultant_path(consultant)
I know the easy solution would just be this;
get 'consultant/:slug' => 'consultants#show', as: :consultant
But i want the url to be specific.
And the constraints: {host: a-domain.com} unfortunately does not allow for domain-specific routing, since only one as: :consultant can exist in routes.rb.
Routes don't actually have anything to do with your models. Your routes are the external REST API of your application while your models are an internal implementation detail.
I would just set the routes up as:
This just describes RESTful resources in your application like any other. Your routes should neither know or care that a doctor is a kind of consultant - its just a thing that can be routed to. Nor should it care that you're using slugs, to the router
:idis just some kind of identifier.The only actual connection between routes and models are the polymorphic routing helpers which basically just look up the the name of routing helper method to call based on convention over configuration:
When you pass a model instance Rails 'calls
model_name.route_keyon the model instance and then will determine if its singular or plural by checking if the model has been persisted.If you want the polymorphic routing helpers to "just work" one solution is using Single Table Inheritance:
When generating links you won't actually have to care about the type:
When you pass an instance of Doctor it will use
doctor_pathand when you pass an instance of Lawyer you getlawyer_path.It also works for forms:
You can also acheive the same thing with the decorator pattern if STI isn't your cup of tea.