views:

54

answers:

3

[Rails] Where to put this code?

I have a user1 and when another registered user2 sees the profile of user1, has some buttons on it: ['add as friend', 'give me your number', 'give me your email', 'ask her out', 'view photos']. The 1,2,3,4 are POST, with AJAX. Now, i have to make a new controller named 'ProfileActionsController' or i should put this code in the 'UsersController'?

or maybe a another posiibility? thanks ;)

+1  A: 

You can do both. To avoid UsersController from becoming too bulky you should put it new controller which will help for maintainance .

Salil
thanksive saw someone have more folders in the app folder. normal is models controller view.. but they have the presenter too and another one i dont remember.. you know something about this?
Totty
A: 

If it's an action made on an user (i.e., that in someway modifies a user through its model), then ideally you should put those actions inside the users_controller.

jgradim
A: 

You will most likely have to store some of these relationships in different database tables. For example, User has_many :friends. This design encourages a Friend model. Which leads to a FriendsController and to urls like POST /user/1/friend to create a friendship between the current user (user2) and user 1.

Those belong in a separate controller.

If you need more Ajax actions on a user, defining them in UsersController is the right place. "Give me your number", "Give me your email" and "View Photos", depending on requirements, could be hidden sections of the html, or simple Ajax GET requests to the UserController to render partials or JSON.

Those can stay on the UserController

GENERAL ADVICE: Always try to stay within the 7 actions for each controller (new, create, edit, update, index, show, destroy) - when you feel you need to define your own action, think about which of the 7 it is closest to. Can it be combined gracefully? If not, then is it acting on a separate concept?

Jonathan Julian
i have controllers with more than 7 actions, how may i do them to work good with more, the code become a little bit confusing...i have fat models and thin controllers. the models are really really fat.. :s i heard about some more folders in the app folder, like presenter or something like this, but i dont remember well..
Totty