# Restful Paradigm: How do I know when to create a new controller?

**URL:** <https://www.ruby-forum.com/t/restful-paradigm-how-do-i-know-when-to-create-a-new-controller/142080>\
**Category:** Rails\
**Created:** [July 3, 2008, 9:59am UTC](https://www.ruby-forum.com/t/restful-paradigm-how-do-i-know-when-to-create-a-new-controller/142080 "2008-07-03T09:59:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 3, 2008, 9:59am UTC](https://www.ruby-forum.com/t/restful-paradigm-how-do-i-know-when-to-create-a-new-controller/142080/1 "2008-07-03T09:59:18Z")

</div>

I’ve read a lot of articles and tutorials about how to use Rest, but  
one thing I haven’t seen in many of them is when to create  
controllers. In many cases I have controllers with only 1 method in  
them (usually a create), and I’m stuck wondering if it’s ok like that  
or if it should be a custom method inside another controller that has  
a close relationship (usually a has\_many) with it.

Also, how do I know when I’m correctly following the Restful paradigm?  
I’ve learned how to do it, but I haven’t learned the theory behind it  
or anything like that. Any help would be appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 3, 2008, 10:39am UTC](https://www.ruby-forum.com/t/restful-paradigm-how-do-i-know-when-to-create-a-new-controller/142080/2 "2008-07-03T10:39:48Z")

</div>

Difficult question. In some cases it’s more a matter of  
taste, if you prefer the “canonical” way and have a file  
more or if you like it more “compact”.  
In our company we would most likely create a controller,  
whenever one of the CRUD actions can be applied to  
a resource.  
The rough rule table = resource = controller works quite  
well most of the time and you know what to expect from  
the code. Have a products table? It will be mainly handled  
in the products controller. (The resource/route for it most  
likely nested in the “categories” resources if we have one.

Only some minor and directly related methods stay in the  
controller, especially the non-crud kind like if you have invoices  
the print, write-pdf, book, export, cancel or whatever.

That’s only rough rules of course. But it works, especially in a team.  
Just don’t think much about it, create the controller and everybody  
knows where to look for what.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 3, 2008, 3:39pm UTC](https://www.ruby-forum.com/t/restful-paradigm-how-do-i-know-when-to-create-a-new-controller/142080/3 "2008-07-03T15:39:23Z")

</div>

+1  
Müller

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 4, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/restful-paradigm-how-do-i-know-when-to-create-a-new-controller/142080/4 "2008-07-04T15:54:14Z")

</div>

David,

thank you for your post and for the blogs. Very helpful and timely.

I am working on an area in my app which has made me think very  
carefully about how to structure things and how to set up the  
routing. Each time I think more about it I get nearer to a clean  
solution. The thing I think I face is separating the external  
consideration of the resource and the representation from the data and  
program structure itself. After reading your blog, I am coming to  
realise that these are very separate considerations.

I am writing an office app in which all navigation is based on  
buttons, and links. (Although I guess that is true of most sites  
too.) What this means is that I tend to think more about how to make  
something work in the view and how to travel between views without  
really considering the aspect of resources.

I have need to extend existing models where customer orders are  
received already created from external sources and to now allow local  
creation of orders. Whilst it sounds simple, there are constraints  
that have led me to partition the customers/orders. Hence there are  
some parts of the MVC that are shared and some that are distinct.

I find deriving the routing scheme very difficult and am coming to a  
suitable solution very slowly. It just seems to me that there is an  
area here for some notation and dare I say it methodology that would  
help in the process of mapping from the requirement to the development  
of resources and then the mapping through the routing to the  
application and structure. I sense there could even be template  
solutions that could fit a wide range of requirements and that there  
are a lot of us out there at the moment sweating over similar  
problems. I guess it will begin to fall into place in time. Blogs  
like yours do help.

As an aside, once I have created the routes, I do not always find the  
the use of the path and url methods terribly intuitive. We have rake  
routes, and I was wondering if there might be any way to generate the  
formats available for path and url methods for a resource or group of  
resources.

Tonypm

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 4, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/restful-paradigm-how-do-i-know-when-to-create-a-new-controller/142080/5 "2008-07-04T22:12:25Z")

</div>

Ok - so I was talking nonsense. The path/url method format is in rake  
routes display too.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 3, 2008, 4:38pm UTC](https://www.ruby-forum.com/t/restful-paradigm-how-do-i-know-when-to-create-a-new-controller/142080/6 "2008-07-03T16:38:02Z")

</div>

Hi –

On Thu, 3 Jul 2008, Mike C wrote:

> I’ve read a lot of articles and tutorials about how to use Rest, but  
> one thing I haven’t seen in many of them is when to create  
> controllers. In many cases I have controllers with only 1 method in  
> them (usually a create), and I’m stuck wondering if it’s ok like that  
> or if it should be a custom method inside another controller that has  
> a close relationship (usually a has\_many) with it.
> 
> Also, how do I know when I’m correctly following the Restful paradigm?  
> I’ve learned how to do it, but I haven’t learned the theory behind it  
> or anything like that. Any help would be appreciated, thanks.

If you haven’t already seen them, you might find these interesting:

[http://dablog.rubypal.com/2008/3/23/splitting-hairs-over-resource](http://dablog.rubypal.com/2008/3/23/splitting-hairs-over-resource)  
[http://dablog.rubypal.com/2008/4/24/splitting-hairs-over-resource-part-2](http://dablog.rubypal.com/2008/4/24/splitting-hairs-over-resource-part-2)

They’re about the REST concepts of resource and representation, and  
how they map to Rails’s REST-friendly facilities, as well as some  
common misunderstandings.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
Intro to Ruby on Rails July 21-24 Edison, NJ  
Advancing With Rails August 18-21 Edison, NJ  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
