# Nested Rails Views

**URL:** <https://www.ruby-forum.com/t/nested-rails-views/182808>\
**Category:** Rails\
**Created:** [February 3, 2010, 3:19am UTC](https://www.ruby-forum.com/t/nested-rails-views/182808 "2010-02-03T03:19:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [February 3, 2010, 3:19am UTC](https://www.ruby-forum.com/t/nested-rails-views/182808/1 "2010-02-03T03:19:38Z")

</div>

I’m at the point where I have a lot of view files for each controller in  
my app. I’d like to be able to add another level or two of directories  
in my view folders just for organizational purposes. What’s the best  
way to do this?

i.e. app/views/controller/sub\_dir/view.html.erb

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [February 3, 2010, 3:56am UTC](https://www.ruby-forum.com/t/nested-rails-views/182808/2 "2010-02-03T03:56:50Z")

</div>

Raymond O’Connor wrote:

> I’m at the point where I have a lot of view files for each controller in  
> my app.

Why do you have so many? It’s rare to have more than about 5 views per  
controller.

> I’d like to be able to add another level or two of directories  
> in my view folders just for organizational purposes. What’s the best  
> way to do this?
> 
> i.e. app/views/controller/sub\_dir/view.html.erb

Answer the first question, and perhaps an idea will present itself.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [February 3, 2010, 5:10am UTC](https://www.ruby-forum.com/t/nested-rails-views/182808/3 "2010-02-03T05:10:08Z")

</div>

Marnen Laibow-Koser wrote:

> Raymond O’Connor wrote:
> 
> > I’m at the point where I have a lot of view files for each controller in  
> > my app.
> 
> Why do you have so many? It’s rare to have more than about 5 views per  
> controller.
> 
> > I’d like to be able to add another level or two of directories  
> > in my view folders just for organizational purposes. What’s the best  
> > way to do this?
> > 
> > i.e. app/views/controller/sub\_dir/view.html.erb
> 
> Answer the first question, and perhaps an idea will present itself.
> 
> Best,  
> –Â  
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

I could split the actions into multiple controllers, but then I’d want  
some sort of organization to group controllers then… Once a project  
reaches a certain size, actions tend to organize hierarchically at least  
my project has. Just wondering if there’s an easy way to organize all  
those files hierarchically since their logical group is hierarchical

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [February 3, 2010, 7:02am UTC](https://www.ruby-forum.com/t/nested-rails-views/182808/4 "2010-02-03T07:02:53Z")

</div>

Raymond O’Connor wrote:

> Marnen Laibow-Koser wrote:
> 
> > Raymond O’Connor wrote:
> > 
> > > I’m at the point where I have a lot of view files for each controller in  
> > > my app.
> > 
> > Why do you have so many? It’s rare to have more than about 5 views per  
> > controller.
> > 
> > > I’d like to be able to add another level or two of directories  
> > > in my view folders just for organizational purposes. What’s the best  
> > > way to do this?
> > > 
> > > i.e. app/views/controller/sub\_dir/view.html.erb
> > 
> > Answer the first question, and perhaps an idea will present itself.
> > 
> > Best,  
> > –Â  
> > Marnen Laibow-Koser  
> > [http://www.marnen.org](http://www.marnen.org)  
> > removed\_email\_address@domain.invalid
> 
> I could split the actions into multiple controllers,

That’s a more common plan. Please give some more detail about how your  
app is structured, and perhaps we can give more specific help.

> but then I’d want  
> some sort of organization to group controllers then…

Rails provides for namespacing controllers.

> Once a project  
> reaches a certain size, actions tend to organize hierarchically at least  
> my project has.

Yes. Usually controllers are the first level in the hierarchy.

> Just wondering if there’s an easy way to organize all  
> those files hierarchically since their logical group is hierarchical

Again: what’s your app like? Why do you have so many views per  
controller? Please give specific answers if you want better help.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [February 4, 2010, 1:37am UTC](https://www.ruby-forum.com/t/nested-rails-views/182808/5 "2010-02-04T01:37:48Z")

</div>

Thanks for the help so far. Here’s a more concrete example of what I’m  
talking about. I have an app used for warehousing and fulfillment.  
Some of the controller domains are:

Receiving  
Locating Items  
Shipping Out Items  
Finance Related Stuff  
Shipment Tracking

Within just the Receiving controller there are dozens of view files  
because it’s become larger and larger over the years. You need to be  
able to receive a shipment, then receiving individual items in a  
shipment, track exception cases, edit previous check in data, etc. I’d  
really like a way to organize some of those files such that I have  
/receiving/shipment\_check\_in/…, /receiving/exceptions/…,  
/receiving/item\_check\_in/…

All my controllers are getting to this size or are already there. I was  
just wondering if there was an easy way to refractor and organize  
projects of these size.

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [February 4, 2010, 4:25am UTC](https://www.ruby-forum.com/t/nested-rails-views/182808/6 "2010-02-04T04:25:18Z")

</div>

Raymond O’Connor wrote:

> Thanks for the help so far. Here’s a more concrete example of what I’m  
> talking about. I have an app used for warehousing and fulfillment.  
> Some of the controller domains are:
> 
> Receiving  
> Locating Items  
> Shipping Out Items  
> Finance Related Stuff  
> Shipment Tracking
> 
> Within just the Receiving controller there are dozens of view files  
> because it’s become larger and larger over the years. You need to be  
> able to receive a shipment, then receiving individual items in a  
> shipment, track exception cases, edit previous check in data, etc. I’d  
> really like a way to organize some of those files such that I have  
> /receiving/shipment\_check\_in/…, /receiving/exceptions/…,  
> /receiving/item\_check\_in/…
> 
> All my controllers are getting to this size or are already there. I was  
> just wondering if there was an easy way to refractor and organize  
> projects of these size.

Your controllers are _far_ too big and general. Receiving should not be  
one controller. You’d probably want a ShipmentsController, an  
ExceptionCasesController, and so on.

You could probably benefit from redesigning your controllers to follow a  
RESTful pattern. You needn’t adhere slavishly to it, but using REST as  
a guideline will give you a better idea of what controllers are normally  
expected to do.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![nappin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f475e1/32.png) [@nappin](https://www.ruby-forum.com/u/nappin)\
**Post date:** [February 3, 2010, 9:11pm UTC](https://www.ruby-forum.com/t/nested-rails-views/182808/7 "2010-02-03T21:11:43Z")

</div>

Since you don’t have many details posted, I’ll suggest a generic  
solution that worked for me:

Use partials. You can populate them based on sub-categories within  
your actions or groups or whatever your ‘parent’ classification is.  
You can implement whatever heirarchy you like - each level would have  
a partial, that would then render the child levels as needed. Render  
using conditionals or a loop, and you can turn three partials into a  
three-level-deep, nested heirarchy.

Hope that helps. Post details for better assistance.

SR
