Reputation: 15831
[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 ;)
Upvotes: 0
Views: 135
Reputation: 12264
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?
Upvotes: 1
Reputation: 2871
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.
Upvotes: 0
Reputation: 47472
You can do both. To avoid UsersController from becoming too bulky you should put it new controller which will help for maintainance .
Upvotes: 1