Subfolders in Rails applications (newbie)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Subfolders in Rails applications (newbie)

PWills
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/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
4. Create folder 'accounting' in /app/views/
5. Create folder 'accounts' in /app/views/accounting/
6. In /app/views/accounting, create 'index.rhtml'
   <h1>Testing accounting accounts index</h1>
7. Create folder 'accounting' in /app/helpers/
8. 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, I ge the following error:

LoadError in <controller not set>#<action not set>
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

which violates the principle of pretty urls.


--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Subfolders in Rails applications (newbie)

Tom Lieber
On 12/17/05, PWills <[hidden email]> 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 Lieber
[hidden email]
http://AllTom.com/
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Subfolders in Rails applications (newbie)

Jamie Macey
In reply to this post by PWills
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


On Sat, 2005-12-17 at 21:34 +0100, PWills 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...)
>
> I would like my rails application to have controllers logically grouped
> into folders. So for instance:
>
> http://domain.com/accounting/accounts
> 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
> 4. Create folder 'accounting' in /app/views/
> 5. Create folder 'accounts' in /app/views/accounting/
> 6. In /app/views/accounting, create 'index.rhtml'
>    <h1>Testing accounting accounts index</h1>
> 7. Create folder 'accounting' in /app/helpers/
> 8. 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, I ge the following error:
>
> LoadError in <controller not set>#<action not set>
> 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
>
> which violates the principle of pretty urls.
>
>

_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails