# What is the role of the controllers? (Advanced question)

**URL:** <https://www.ruby-forum.com/t/what-is-the-role-of-the-controllers-advanced-question/107250>\
**Category:** Rails\
**Created:** [June 19, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/what-is-the-role-of-the-controllers-advanced-question/107250 "2007-06-19T17:33:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [June 19, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/what-is-the-role-of-the-controllers-advanced-question/107250/1 "2007-06-19T17:33:40Z")

</div>

I am not a noob, and this is actually I pretty “deep” question and it’s  
been really kicking my ass. I wanted to get some feedback from other  
experienced rails developers. Any help would be greatly appreciated. I  
would even pay for some feedback to this. So here it goes…

I have noticed a lot of people making a controller for each model. The  
point of this is to keep all controller code related to that model in  
that controller. This allows me to set up routes as follows:

map.my\_account ‘my\_account/:action’, :controller =\> “users”  
map.register ‘register’, :controller =\> “users”, :action =\> “new”  
map.checkout ‘checkout/:action’, :controller =\> “orders”, :action =\>  
“new”

So far this seems great and clean, but I ran into a problem doing this.  
When I was doing the checkout I needed to have a user login or register  
right there, I should not have to redirect them to the login or  
registration page. So I made a partial with the user registration form  
and included it in both areas. The million dollar question is where does  
that registration form action point? Does it point to /register or does  
it point to checkout/register? If it goes to checkout/register I have  
violated the rule of keeping all user code in the users controller,  
because now I am registering a user in the orders controller (my  
checkout route points there). Another problem is that the  
my\_account/register action simply redirects me to the home page after  
registering. The checkout/register sends back javascript to update HTML  
on the page. So now I have 2 areas on my site wanting to do the same  
thing but expecting 2 different responses. So I could do one of 2  
things:

1. I could set up another route:  
map. checkout\_register ‘checkout/register’, :controller =\> “users”,  
:action =\> “new”  
Then in the users controller I could see if they are registering from  
/register or checkout/register and render the appropriate response based  
on that.

2. I could make a method in my application\_controller called  
register\_user and then just use that method in both the users/new method  
and the checkout/register method. Now each controller can render  
whatever response it wants.

#2 seems to be the best in my opinion. This situation has defeated the  
whole idea of making controllers to handle all code for that model  
though. Here is the ultimate question:

Does a controller represent a set of methods that perform actions on a  
similar piece of data (ex: a user object)? Or does a controller  
represent a set of methods that help prepare data for views that are  
related (as in my example above, you could actually be registering a  
user in more than one controller)?

Sorry for the long question, thanks for your help.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [June 19, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/what-is-the-role-of-the-controllers-advanced-question/107250/2 "2007-06-19T19:49:26Z")

</div>

This may not answer your bigger question, but to the specific example,  
you could stash the referrer url in the session, go to the  
registration and login, and then redirect back to the saved location  
rather than the home page. In restful\_authentication plugin this is  
what the store\_location and redirect\_back\_or\_default method do. Then  
your controller actions stay separate.

linoj

On Jun 19, 11:33 am, Ben J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [June 19, 2007, 7:53pm UTC](https://www.ruby-forum.com/t/what-is-the-role-of-the-controllers-advanced-question/107250/3 "2007-06-19T19:53:28Z")

</div>

Ben J. wrote:

> I have noticed a lot of people making a controller for each model. The  
> point of this is to keep all controller code related to that model in  
> that controller. This allows me to set up routes as follows:

This basic assumption is where you go wrong IMHO. There is no rule  
saying anything about the relationship between models and controllers.  
Actually one point of having models and controllers separated is that  
the model should be easily accessible from more than one location. Stop  
worrying about it and use your models where you need them.

> Does a controller represent a set of methods that perform actions on a  
> similar piece of data (ex: a user object)? Or does a controller  
> represent a set of methods that help prepare data for views that are  
> related (as in my example above, you could actually be registering a  
> user in more than one controller)?

You can create controllers for manipulating similar data or for related  
views. Do whatever feels most natural and pain free.

On a related note, check out some of the blogposts on skinny controller  
and fat models floating around the blogosphere, they might give you a  
solid perspective on things.

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [June 20, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/what-is-the-role-of-the-controllers-advanced-question/107250/4 "2007-06-20T21:04:33Z")

</div>

Note also that if you define a login method in application.rb, it is  
accessible to any controller in your app. Again, store\_location, or  
any similar means or remembering the intended destination will work  
fine to move the user along to the right page after they log in.

On Jun 19, 2007, at 8:33 AM, Ben J. wrote:

> registration page. So I made a partial with the user registration form  
> HTML  
> based  
> #2 seems to be the best in my opinion. This situation has defeated the
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Steve R.  
removed\_email\_address@domain.invalid  
[http://www.calicowebdev.com](http://www.calicowebdev.com)
