# Subfolders in Rails applications (newbie)

**URL:** <https://www.ruby-forum.com/t/subfolders-in-rails-applications-newbie/50535>\
**Category:** Rails\
**Created:** [December 17, 2005, 9:34pm UTC](https://www.ruby-forum.com/t/subfolders-in-rails-applications-newbie/50535 "2005-12-17T21:34:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PWills](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/53a042/32.png) [@PWills](https://www.ruby-forum.com/u/PWills)\
**Post date:** [December 17, 2005, 9:34pm UTC](https://www.ruby-forum.com/t/subfolders-in-rails-applications-newbie/50535/1 "2005-12-17T21:34:09Z")

</div>

After about a week playing with Rails, I have built a couple of apps but  
am still unable to get one thing to work: subfolders. (I am probably  
using the wrong term here, but oh well…)

I would like my rails application to have controllers logically grouped  
into folders. So for instance:

[http://domain.com/accounting/accounts](http://domain.com/accounting/accounts)  
[http://domain.com/hr/employees](http://domain.com/hr/employees)

Here’s how I have tried to accomplish this:

1. Create folder ‘accounting’ in /app/controllers/
2. In /app/controllers/accounting, create ‘accounts\_controller.rb’  
class AccountsController \< ApplicationController  
def index  
end  
end
3. Create folder ‘accounting’ in /app/views/
4. Create folder ‘accounts’ in /app/views/accounting/
5. In /app/views/accounting, create ‘index.rhtml’
# Testing accounting accounts index
6. Create folder ‘accounting’ in /app/helpers/
7. In /app/helpers/accounting, create ‘accounts\_helper.rb’  
module AccountsHelper  
end

Then if I fire up WEBrick and navigate to URL  
[http://localhost:3000/accounting/accounts](http://localhost:3000/accounting/accounts), I ge the following error:

LoadError in #  
Already loaded file  
‘./script/…/config/…/app/controllers/accounting/accounts\_controller.rb’  
but ‘AccountingController’ was not set, perhaps you need to rename  
‘./script/…/config/…/app/controllers/accounting/accounts\_controller.rb’?

Any help would be appreciated. Right now I have resorted to having ALL  
of my controllers in the same directory and using grotesquely long  
names, i.e.

[http://domain.com/accounting\_accounts](http://domain.com/accounting_accounts)

which violates the principle of pretty urls.

---

<div class="post-metadata">

**Author:** ![PWills](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/53a042/32.png) [@PWills](https://www.ruby-forum.com/u/PWills)\
**Post date:** [December 18, 2005, 4:55pm UTC](https://www.ruby-forum.com/t/subfolders-in-rails-applications-newbie/50535/2 "2005-12-18T16:55:10Z")

</div>

On 12/17/05, PWills [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> After about a week playing with Rails, I have built a couple of apps but  
> am still unable to get one thing to work: subfolders. (I am probably  
> using the wrong term here, but oh well…)

The default route, ‘:controller/:action’, should give you the URLs you  
ask for without much complication. If you have a controller  
‘accounting’ with an action ‘accounts’ then the URL (given by the  
default route) is ‘/accounting/accounts’.

Sincerely,

Tom L.  
removed\_email\_address@domain.invalid  
[http://AllTom.com/](http://AllTom.com/)

---

<div class="post-metadata">

**Author:** ![PWills](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/53a042/32.png) [@PWills](https://www.ruby-forum.com/u/PWills)\
**Post date:** [December 19, 2005, 7:27pm UTC](https://www.ruby-forum.com/t/subfolders-in-rails-applications-newbie/50535/3 "2005-12-19T19:27:21Z")

</div>

To properly get automatic subfolders like this, you need to put your  
controllers inside a scope.

For example, if /app/controllers/accounting/accounts\_controller.rb is  
supposed to be mapped to accounting/accounts, that file needs to define  
a class like so:

class Accounting::AccountsController \< ApplicationController

Similarly, you want to define your employees controller as:

class Hr::EmployeesController \< ApplicationController

Just be aware that linking between scopes is a little tricky sometimes,  
but you should be able to get it with some trial and error (also, tests  
are your friend)

The other option if you don’t have many controllers would be to hard  
code some routes with the longer controller name.

- Jamie
