Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@JurgenJocubeit

This comment has been minimized.

Show comment
Hide comment
@JurgenJocubeit

JurgenJocubeit 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

JurgenJocubeit 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

This comment has been minimized.

Show comment
Hide comment
@kulesa

kulesa May 9, 2012

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

Owner

kulesa commented May 9, 2012

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

@JurgenJocubeit

This comment has been minimized.

Show comment
Hide comment
@JurgenJocubeit

JurgenJocubeit 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

JurgenJocubeit 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

This comment has been minimized.

Show comment
Hide comment
@kulesa

kulesa May 9, 2012

Hey, that's awesome! I've updated my routes file and it works.

Owner

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