Given that I have controllers:
app/controllers/app1/users_controller.rb app/controllers/app2/users_controller.rb
I have in my routes file:
["app1", "app2"].each do |n|
constraints(:host => "#{n}.com") do
scope({:as => vn, :module => vn}) do
resources :users
end
end
end
This gives me routes like so:
GET app1_users_path (app1.com/users) { :controller => "app1/users", :action => "index" }
GET app2_users_path (app2.com/users) { :controller => "app2/users", :action => "index" }
I do this for every path, for every “app” in my application.
The problem is, as the no. of both apps and paths grows, so does the no. of paths
n = no_of_paths a = no_of_apps (n * a) = "LOADS"
Can anyone think of a way I can set the “module” part of my routes (the controller prefix) as a wildcard so I only have to name each route once?
Something like:
match ":controller/:action(/:id)" => ":host/:controller#:action"
maybe?
I’ve solved this…
Essentially, I was asking the wrong question.
My application serves more than one website so I thought it was prudent to create a controller (and therefore a route) for each “vertical” (site within my app).
The shared behaviour in these controllers went into a module which I included in each controller. ie:
In practise, I found that 95% of the time I stuck with the shared behaviour.
Having separate controllers and routes was overkill.
Working fine (and faster) now with one instance for most controllers and one or two name-spaced controllers where I need to customise the behaviour.
I’ve also decided to inherit custom controllers from the default behaviour (rather than include modules):