# Rails 3 nested routing without resources

**URL:** <https://www.ruby-forum.com/t/rails-3-nested-routing-without-resources/208831>\
**Category:** Rails\
**Created:** [June 28, 2011, 8:50am UTC](https://www.ruby-forum.com/t/rails-3-nested-routing-without-resources/208831 "2011-06-28T08:50:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [June 28, 2011, 8:50am UTC](https://www.ruby-forum.com/t/rails-3-nested-routing-without-resources/208831/1 "2011-06-28T08:50:06Z")

</div>

It sounds like it is generally recommended either not to nest  
controllers at  
all or at most by one level. However this seems to relate mostly to  
resources. Interactive functionality generally works well with  
resources,  
but for a complete website there is often additional content that  
contains  
information not suitable for resources, but that still uses the same  
views  
mechanism for consistency.

I’ve got some resources and also some content that doesn’t suit using  
resources and it is convenient to store the content in hierarchical  
directories (?namespaces). The content is written as rails views. All  
worked  
OK under rails 2 but I’m struggling to get routing to work under rails  
3.

For example if you have an app/views directory structure such as:

views \>  
level1  
level2  
level3

and would to place views at each of these levels, including an index  
action,  
what is the best way to create route(s) in routes.rb to accommodate  
this?

Using the default wildcard route of match  
‘:controller(/:action(/:id(.:format)))’ results in the top level working  
but  
the index action for level2 results in an action not known for level1  
controller from the router.

I’ve experiemented with various combinations of namespace, scope and  
match  
statements, but none seem to cater for this seemingly simple requirement  
to  
match a nested set of views to urls.

Maybe I’m missing a trick here - is there a better way to structure  
hierarchical non-resource content but still use the facilities in the  
rails  
framework? I guess I could combine rails with some other framework, but  
would like to keep everything within rails if possible.

Any suggestions greatly appreciated.

---

<div class="post-metadata">

**Author:** ![tfp44](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@tfp44](https://www.ruby-forum.com/u/tfp44)\
**Post date:** [June 28, 2011, 2:35pm UTC](https://www.ruby-forum.com/t/rails-3-nested-routing-without-resources/208831/2 "2011-06-28T14:35:27Z")

</div>

Hi,

The problem is very specific, so would be hard to quickly find a  
solution  
with the information you have posted.  
Would be good if you can post the routes file from your Rails 2 app  
which  
used to work fine and the best working version of routes file from Rails  
3  
app.

You may also want to read up the guide on Rails 3 routing -

> **[Rails Routing from the Outside In — Ruby on Rails Guides](https://guides.rubyonrails.org/routing.html)**
>
> Rails Routing from the Outside InThis guide covers the user-facing features of Rails routing.After reading this guide, you will know: How to interpret the code in config/routes.rb. How to construct your own routes, using either the preferred...

Read section - 3.11 - Route Globbing, that may do the trick for you.

---

<div class="post-metadata">

**Author:** ![tfp44](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@tfp44](https://www.ruby-forum.com/u/tfp44)\
**Post date:** [June 29, 2011, 12:11pm UTC](https://www.ruby-forum.com/t/rails-3-nested-routing-without-resources/208831/3 "2011-06-29T12:11:54Z")

</div>

Hi Chirag,

Thanks for the response.

Equivalent routes from rails 2 that seem to have worked fine are:

map.connect ‘level1/level2/level3/:action’, :controller =\> ‘level1/  
level2/level3’  
map.connect ‘level1/level2/:action’, :controller =\> ‘level1/level2’  
map.connect ‘:controller/:action/:id’

Further messing around I came up with:

namespace “levelx” do  
namespace “levely” do  
namespace “levelz” do  
match ‘levelzcontrollerone(/:action)’ =\> ‘levelzcontrollerone#’  
match ‘:action’ =\> ‘#’  
root :to =\> ‘levelz#index’  
end  
match ‘levelycontrollerone(/:action)’ =\> ‘levelycontrollerone#’  
match ‘:action’ =\> ‘#’  
root :to =\> ‘levely#index’  
end  
match ‘levelxcontrollerone(/:action)’ =\> ‘levelxcontrollerone#’  
match ‘:action’ =\> ‘#’  
root :to =\> ‘levelx#index’  
end

Which seems to work. rake routes gives:

```
 levelx_levely_levelz /levelx/levely/levelz/levelzone(/:action)

```

(.:format) {:controller=\>“levelx/levely/levelz/levelzone”}  
/levelx/levely/  
levelz/:action(.:format) {:controller=\>“levelx/levely/  
levelz”}  
levelx\_levely\_levelz\_root /levelx/levely/  
levelz(.:format) {:controller=\>“levelx/levely/  
levelz/levelz”, :action=\>“index”}  
levelx\_levely /levelx/levely/levelyone(/:action)  
(.:format) {:controller=\>“levelx/levely/levelyone”}  
/levelx/  
levely/:action(.:format) {:controller=\>“levelx/  
levely”}  
levelx\_levely\_root /levelx/  
levely(.:format) {:controller=\>“levelx/  
levely/levely”, :action=\>“index”}  
levelx /levelx/levelxone(/:action)  
(.:format) {:controller=\>“levelx/levelxone”}  
/  
levelx/:action(.:format)  
{:controller=\>“levelx”}  
levelx\_root /  
levelx(.:format)  
{:controller=\>“levelx/levelx”, :action=\>“index”}  
/:controller(/:action)(.:format)

Not sure if this is in any way sensible but seems to get the job done.

Thanks once again for your help

---

<div class="post-metadata">

**Author:** ![tfp44](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@tfp44](https://www.ruby-forum.com/u/tfp44)\
**Post date:** [June 29, 2011, 6:56pm UTC](https://www.ruby-forum.com/t/rails-3-nested-routing-without-resources/208831/4 "2011-06-29T18:56:45Z")

</div>

A direct conversion to the routes you had in Rails 2, would be something  
like this:

match ‘level1/level2/level3/:action’, :controller =\>  
‘level1/level2/level3’  
match ‘level1/level2/:action’, :controller =\> ‘level1/level2’  
match ‘:controller/:action/:id’

There would be a better way of doing this with namespace or scope, but  
if  
this works fine, then let it be.

---

<div class="post-metadata">

**Author:** ![tfp44](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@tfp44](https://www.ruby-forum.com/u/tfp44)\
**Post date:** [June 28, 2011, 1:20pm UTC](https://www.ruby-forum.com/t/rails-3-nested-routing-without-resources/208831/5 "2011-06-28T13:20:31Z")

</div>

Hello all,

Don’t suppose anyone has a moment to give me a pointer on this one?

I’ve tried the following:

namespace “levelx” do  
namespace “levely” do  
namespace “levelz” do  
root :to =\> ‘levelz#index’  
end  
root :to =\> ‘levely#index’  
end  
root :to =\> ‘levelx#index’’  
end

This provides default routes for each namespace, but you then can’t  
access  
any other controllers at each level of namespace. You can’t specify a  
:controller segment, so adding match “:controller(/:action)” within each  
namespace block is not an option.

I’ve also tried:

match ‘/levelx/levely/levelz/:controller(/:action)’  
match ‘/levelx/levely/levelz’ =\> ‘levelx/levely/levelz#index’  
match ‘/levelx/levely/:controller(/:action)’  
match ‘/levelx/levely’ =\> ‘levelx/levely#index’  
match ‘/levelx/:controller(/:action)’  
match ‘/levelx’ =\> ‘levelx#index’

Which again seems to route index actions OK but not other controllers  
within  
each namespace.

I’ve tried:

match ‘/levelx/levely/levelz/:controller(/:action)’, :controller =\>  
/levelx/levely/levelz\*/  
match ‘/levelx/levely/:controller(/:action)’, :controller =\>  
/levelx/levely\*/  
match ‘/levelx/:controller(/:action)’, :controller =\> /levelx/levely\*/  
match ‘/:controller(/:action)’, :controller =\> /levelx\*/

Which doesn’t seem to work.

What I’m really after is the equivalent of the match  
‘/:controller(/:action)’ that will work with nested namespaces. If  
constraints can be applied to top level namespaces then so much the  
better.

Maybe time to admit defeat and abandon rails for this type of website,  
which  
would be a shame since the rails framework is great for teh rest of the  
resource-based parts of the site. More likely I’m just being thick.

Many thanks for any thoughts.

---

<div class="post-metadata">

**Author:** ![tfp44](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@tfp44](https://www.ruby-forum.com/u/tfp44)\
**Post date:** [June 29, 2011, 11:03pm UTC](https://www.ruby-forum.com/t/rails-3-nested-routing-without-resources/208831/6 "2011-06-29T23:03:37Z")

</div>

On Tue, Jun 28, 2011 at 4:18 AM, tfp44 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> I’ve also tried:

As a Rails best practice, I would recommend that you don’t nest routes  
that  
are more than two levels deep.

-Conrad
