Skip to content

Instantly share code, notes, and snippets.

@kulesa
Forked from dhh/gist:2492118
Created April 27, 2012 09:44
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kulesa/2507892 to your computer and use it in GitHub Desktop.
Save kulesa/2507892 to your computer and use it in GitHub Desktop.
nested routes for namespaces
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
@jocubeit
Copy link

jocubeit commented May 9, 2012

How would you achieve the same thing with a scope instead of a namespace?

I tend not to use named routes with the namespace in them.

eg.

BCX::Application.routes.draw do  
  scope "/admin" do
    draw :projects # => config/routes/admin/projects.rb
  end
end

@kulesa
Copy link
Author

kulesa commented May 9, 2012

Replacing @scope[:shallow_prefix] with @scope[:path] should do the trick, but I haven't tested this.

@jocubeit
Copy link

jocubeit commented May 9, 2012

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

@kulesa
Copy link
Author

kulesa commented May 9, 2012

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