# Administrative Functions: Best Practices?

**URL:** <https://www.ruby-forum.com/t/administrative-functions-best-practices/115899>\
**Category:** Rails\
**Created:** [September 30, 2007, 1:17am UTC](https://www.ruby-forum.com/t/administrative-functions-best-practices/115899 "2007-09-30T01:17:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebacipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c4cdca/32.png) [@sebacipo](https://www.ruby-forum.com/u/sebacipo)\
**Post date:** [September 30, 2007, 1:17am UTC](https://www.ruby-forum.com/t/administrative-functions-best-practices/115899/1 "2007-09-30T01:17:54Z")

</div>

I haven’t come across an obvious best way to write my administrative  
functions. Some candidates are:

- Put the admin functions amongst the standard authenticated user  
functionality of the site, following the REST approach until your dying  
breath, and using roles to expose/hide these functions
- Write admin controllers in an admin:: namespace, with their own  
administrative-specific views
- Write a separate admin application entirely, using the same models,  
and stick the admin app on a subdomain

Each solution has its pros and cons. Certainly someone has opinions  
regarding this; can you convince me/give me instruction as to your  
fondest approach?

---

<div class="post-metadata">

**Author:** ![sebacipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c4cdca/32.png) [@sebacipo](https://www.ruby-forum.com/u/sebacipo)\
**Post date:** [October 1, 2007, 8:34am UTC](https://www.ruby-forum.com/t/administrative-functions-best-practices/115899/2 "2007-10-01T08:34:53Z")

</div>

I’d like to second this question… I was searching around before  
posting this question myself. I have seen posts like this online,  
[http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails](http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails)

- saying that you shouldn’t use namespacing. What would doing it  
using rails routes look like instead?

Thoughts?

On Sep 29, 7:17 pm, Sebastian Mathews \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![sebacipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c4cdca/32.png) [@sebacipo](https://www.ruby-forum.com/u/sebacipo)\
**Post date:** [October 1, 2007, 1:22pm UTC](https://www.ruby-forum.com/t/administrative-functions-best-practices/115899/3 "2007-10-01T13:22:50Z")

</div>

There’s a namespace routing feature in the 2.0 preview release. DHH’s  
blog post about the changes in 2.0  
([Ruby on Rails — Rails 2.0: Preview Release](http://weblog.rubyonrails.org/2007/9/30/rails-2-0-0-preview-release))  
has a short example of how to use it for admin interfaces.

---

<div class="post-metadata">

**Author:** ![sebacipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c4cdca/32.png) [@sebacipo](https://www.ruby-forum.com/u/sebacipo)\
**Post date:** [October 1, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/administrative-functions-best-practices/115899/4 "2007-10-01T16:21:44Z")

</div>

Matt G. wrote:

> There’s a namespace routing feature in the 2.0 preview release. DHH’s  
> blog post about the changes in 2.0  
> ([Ruby on Rails — Rails 2.0: Preview Release](http://weblog.rubyonrails.org/2007/9/30/rails-2-0-0-preview-release))  
> has a short example of how to use it for admin interfaces.

I will need admin functionality in a few upcoming projects and am  
running into the same questions for where it should go. I thought of  
creating a separate Rails app to use on an admin subdomain, but that  
seems like overkill.

I noticed in the inline documentation for the Controller generator  
(script/generate controller --help) that it mentioned the ability to  
“create a controller within a module,” but I couldn’t find any further  
info on what a “module” was in this regard? Here’s the exact text:

# script/generate controller --help

Description:  
The controller generator creates stubs for a new controller and its  
views.

```
The generator takes a controller name and a list of views as 

```

arguments.  
The controller name may be given in CamelCase or under\_score and  
should  
not be suffixed with ‘Controller’. To create a controller within a  
module, specify the controller name as ‘module/controller’.

```
The generator creates a controller class in app/controllers with 

```

view  
templates in app/views/controller\_name, a helper class in  
app/helpers,  
and a functional test suite in test/functional.

Example:  
./script/generate controller CreditCard open debit credit close

```
Credit card controller with URLs like /credit_card/debit.
    Controller: app/controllers/credit_card_controller.rb
    Views: app/views/credit_card/debit.rhtml [...]
    Helper: app/helpers/credit_card_helper.rb
    Test: test/functional/credit_card_controller_test.rb

```

Modules Example:  
./script/generate controller ‘admin/credit\_card’ suspend late\_fee

```
Credit card admin controller with URLs /admin/credit_card/suspend.
    Controller: app/controllers/admin/credit_card_controller.rb
    Views: app/views/admin/credit_card/debit.rhtml [...]
    Helper: app/helpers/admin/credit_card_helper.rb
    Test: test/functional/admin/credit_card_controller_test.rb

```

So is “module” just another term for the namespace approach that has  
been mentioned above? Can anyone provide an example of using this?

And even though DHH’s post re: Rails 2.0 has some text that points to a  
new solution, what is a viable solution for the 1.x line?

---

<div class="post-metadata">

**Author:** ![sebacipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c4cdca/32.png) [@sebacipo](https://www.ruby-forum.com/u/sebacipo)\
**Post date:** [October 1, 2007, 5:37pm UTC](https://www.ruby-forum.com/t/administrative-functions-best-practices/115899/5 "2007-10-01T17:37:29Z")

</div>

Chris B. wrote:

> So is “module” just another term for the namespace approach that has  
> been mentioned above? Can anyone provide an example of using this?

I believe so. The biggest reason namespacing controllers is often  
frowned upon is that it can add unnecessary complexity to your app.

> And even though DHH’s post re: Rails 2.0 has some text that points to a  
> new solution, what is a viable solution for the 1.x line?

The simplest thing you can do with 1.x is:

`
map.resources :whatever, :path_prefix => '/admin'
`

---

<div class="post-metadata">

**Author:** ![sebacipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c4cdca/32.png) [@sebacipo](https://www.ruby-forum.com/u/sebacipo)\
**Post date:** [October 1, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/administrative-functions-best-practices/115899/6 "2007-10-01T18:46:39Z")

</div>

Matt G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Chris B. wrote:
> 
> > So is “module” just another term for the namespace approach that has  
> > been mentioned above? Can anyone provide an example of using this?
> 
> I believe so. The biggest reason namespacing controllers is often  
> frowned upon is that it can add unnecessary complexity to your app.

Yes. Modules and namespaces are synonymous. I’ve posted a sample  
application at [http://dev.panesofglass.org/projects/contacts.zip](http://dev.panesofglass.org/projects/contacts.zip). This  
app is using a contacts namespace to enclose a PeopleController and  
CompaniesController. I can’t get the functional tests to work, so any  
help there would be appreciated. The good news is that you can indeed  
create nested resources now.

> > And even though DHH’s post re: Rails 2.0 has some text that points to a  
> > new solution, what is a viable solution for the 1.x line?
> 
> The simplest thing you can do with 1.x is:
> 
> `
> map.resources :whatever, :path_prefix => '/admin'
> `

That’s definitely the easiest approach. It lets you continue to use  
all the regular methods of referencing paths and urls without having  
to edit every object, @object, object\_path or object\_url reference in  
your app. Of course, if you’re going for a more modular approach and  
you don’t scaffold then you probably don’t mind writing that from  
scratch.

---

<div class="post-metadata">

**Author:** ![sebacipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c4cdca/32.png) [@sebacipo](https://www.ruby-forum.com/u/sebacipo)\
**Post date:** [October 1, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/administrative-functions-best-practices/115899/7 "2007-10-01T14:33:18Z")

</div>

I’d stay away from the admin:: namespace, at least if you are ever  
going to use nested resources. I just spent the weekend refactoring an  
application that had been using that namespace, but then I needed  
nested resources and WHAM… no good. DHH’s comments about  
map.namespaces looks promising, though it seems you could easily do  
something similar with an empty AdminController and nested resources.

On Sep 29, 6:17 pm, Sebastian Mathews \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![sebacipo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c4cdca/32.png) [@sebacipo](https://www.ruby-forum.com/u/sebacipo)\
**Post date:** [October 2, 2007, 12:42am UTC](https://www.ruby-forum.com/t/administrative-functions-best-practices/115899/8 "2007-10-02T00:42:32Z")

</div>

As to my testing issues, I think my solution will be to use RSpec,  
which I’ve already planned to use in the future. However, if you want  
to use the built in testing system, not sure what to tell you. I’d  
still be interested to see if anyone knows how to write tests for  
namespaced controllers.
