-
-
Save kulesa/2507892 to your computer and use it in GitHub Desktop.
nested routes for namespaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActionDispatch::Routing::Mapper | |
def draw(routes_name) | |
instance_eval(File.read(Rails.root.join("config/routes", "#{@scope[:shallow_prefix]}", "#{routes_name}.rb"))) | |
end | |
end | |
BCX::Application.routes.draw do | |
draw :projects # => config/routes/projects.rb | |
namespace :admin do | |
draw :projects # => config/routes/admin/projects.rb | |
end | |
root to: 'projects#index' | |
end |
I've been experimenting, and this doesn't work with a simple substitution because scope[:path]
returns the scope prefixed with a slash. I have found that the following works though:
class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes#{@scope[:path]}", "#{routes_name}.rb")))
end
end
BCX::Application.routes.draw do
scope "/admin" do
draw :projects # => config/routes/admin/projects.rb
end
end
Bonus is that it works for namespace
too! :-)
BCX::Application.routes.draw do
namespace :admin do
draw :projects # => config/routes/admin/projects.rb
end
end
Hey, that's awesome! I've updated my routes file and it works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replacing
@scope[:shallow_prefix]
with@scope[:path]
should do the trick, but I haven't tested this.