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:
-
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. -
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.