# Understanding controllers

**URL:** <https://www.ruby-forum.com/t/understanding-controllers/72702>\
**Category:** Rails\
**Created:** [February 24, 2006, 5:41am UTC](https://www.ruby-forum.com/t/understanding-controllers/72702 "2006-02-24T05:41:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg\_MacGregor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Greg\_MacGregor](https://www.ruby-forum.com/u/Greg_MacGregor)\
**Post date:** [February 24, 2006, 5:41am UTC](https://www.ruby-forum.com/t/understanding-controllers/72702/1 "2006-02-24T05:41:45Z")

</div>

You’ll have to forgive me … I’ve been toying with rails for about a  
week  
and although I don’t feel completely lost there’s still a lot that I  
don’t  
understand. Here goes.

Assume I have a controller called admin in app/controllers as well as  
three  
model classes in app/models: product, category, warehouse. How exactly  
do I  
use the admin controller to produce this kind of structure?

[http://example.com/admin/product/](http://example.com/admin/product/)  
[http://example.com/admin/product/edit/1](http://example.com/admin/product/edit/1)  
[http://example.com/admin/category/new](http://example.com/admin/category/new)  
[http://example.com/admin/warehouse/show/16](http://example.com/admin/warehouse/show/16)

… rather than just simply:

[http://example.com/admin/edit/1](http://example.com/admin/edit/1)  
[http://example.com/admin/list](http://example.com/admin/list)  
[http://example.com/admin/show/16](http://example.com/admin/show/16)

Keep in mind that “admin” is password protected and public content is  
accessed like so:

[http://example.com/products/1](http://example.com/products/1)  
[http://example.com/categories/23](http://example.com/categories/23)

etc.

In my first example, are product, category and warehouse controllers as  
well? Or are they actions of the admin controller? If they are actions,  
how  
do I go about creating the CRUD interface that reacts to each action? In  
other words, how do i attach edit, show, list etc actions for each?

Hope my explanation makes sense. Any help appreciated.

---

<div class="post-metadata">

**Author:** ![Greg\_MacGregor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Greg\_MacGregor](https://www.ruby-forum.com/u/Greg_MacGregor)\
**Post date:** [February 24, 2006, 5:51am UTC](https://www.ruby-forum.com/t/understanding-controllers/72702/2 "2006-02-24T05:51:30Z")

</div>

On Thu, 2006-02-23 at 23:39 -0500, Greg MacGregor wrote:

> [http://example.com/admin/product/edit/1](http://example.com/admin/product/edit/1)  
> accessed like so:  
> actions for each?
> 
> Hope my explanation makes sense. Any help appreciated.

* * *

actually, I don’t think you would normally do …

[http://example.com/admin/product/edit/1](http://example.com/admin/product/edit/1)

but would simply do

[http://example.com/admin/edit/1](http://example.com/admin/edit/1)

but since your questions pretty much follow the methodology of the Agile  
book, why not buy the PDF or the book and run through it as it is almost  
done exactly the way you are asking and would probably reduce your  
learning curve.

Craig

---

<div class="post-metadata">

**Author:** ![Greg\_MacGregor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Greg\_MacGregor](https://www.ruby-forum.com/u/Greg_MacGregor)\
**Post date:** [February 24, 2006, 5:54am UTC](https://www.ruby-forum.com/t/understanding-controllers/72702/3 "2006-02-24T05:54:31Z")

</div>

On Thu, Feb 23, 2006 at 11:39:12PM -0500, Greg MacGregor wrote:

> [http://example.com/admin/category/new](http://example.com/admin/category/new)  
> [http://example.com/admin/warehouse/show/16](http://example.com/admin/warehouse/show/16)
> 
> … rather than just simply:
> 
> [http://example.com/admin/edit/1](http://example.com/admin/edit/1)  
> [http://example.com/admin/list](http://example.com/admin/list)  
> [http://example.com/admin/show/16](http://example.com/admin/show/16)  
> […]

You could use just one controller and use custom routes to pick out  
the objects you want to edit.

A better alternative is probably to have individual controllers for  
each object, in an admin module. You can have multiple controllers  
that utilize the same model.

$ ruby script/generate controller “admin/product”

Check out the help by calling each of the generate commands with no  
arguments for more information on this:

$ ruby script/generate controller  
$ ruby script/generate model  
$ ruby script/generate scaffold  
etc…

–  
- Adam

\*\* Expert Technical Project and Business Management  
\*\*\*\* System Performance Analysis and Architecture  
\*\*\*\*\*\* [[http://www.everylastounce.com](http://www.everylastounce.com) ]

[[Adam Fields (weblog) - - entertaining hundreds of millions of eyeball atoms every day](http://www.aquick.org/blog) ] … Blog  
[[Adam Fields Resume](http://www.adamfields.com/resume.html) ]… Experience  
[[Adam Fields | Flickr](http://www.flickr.com/photos/fields) ] … Photos  
[[http://www.aquicki.com/wiki](http://www.aquicki.com/wiki) ]…Wiki  
[[http://del.icio.us/fields](http://del.icio.us/fields) ] … Links

---

<div class="post-metadata">

**Author:** ![Greg\_MacGregor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Greg\_MacGregor](https://www.ruby-forum.com/u/Greg_MacGregor)\
**Post date:** [February 24, 2006, 6:00am UTC](https://www.ruby-forum.com/t/understanding-controllers/72702/4 "2006-02-24T06:00:33Z")

</div>

Funny thing is that I have the book open in front of me! In the examples  
within, something like admin/edit assumes:

def edit  
@product = Product.find(params[:id])  
end

… Ok. No sweat. But what if i don’t want to edit a product … how  
about a  
catgeory instead?

Greg

On 2/23/06, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > actions, how do I go about creating the CRUD interface that reacts to
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Greg MacGregor  
Web D.  
416-516-0395  
www.sixminutes.ca

---

<div class="post-metadata">

**Author:** ![Greg\_MacGregor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Greg\_MacGregor](https://www.ruby-forum.com/u/Greg_MacGregor)\
**Post date:** [February 24, 2006, 6:03am UTC](https://www.ruby-forum.com/t/understanding-controllers/72702/5 "2006-02-24T06:03:38Z")

</div>

def edit\_cat  
@category = Category.find(params[:id])  
end

[http://example.com/admin/edit\_cat/1](http://example.com/admin/edit_cat/1)

assuming of course that you have a categories table/category model

Craig
