The organization of controllers in Rails

0 like 0 dislike
How do you organize controllers?
Let's have a model User, Question, Answer, and accordingly, the controllers for them. In addition to the standard CRUD operations as well, for example:
  1. to questions posed by a specific user GET /users/:user_id/questions
  2. get all the answers of a user GET /users/:user_id/questions/:question_id/answers
  3. to sign the user in question(mean habtm relationship) — ?
  4. create a question from a user(let's say Question#create creates the question is not associated with the user) POST /users/:user_id/questions
  5. edit the question associated with the user (useful in case you need to be sure that the user exists) — PUT /users/:user_id/questions/:id

Important: I know how to make ranting, and no matter how adequate the methods is just an example, no need to delve into logic.

Interested in how you organize the controllers. I see the following options:
Interested in how you organize the controllers. I see the following options:
1. a controller
1.1 and only basic methods (i.e. CRUD) if, for example, for example 1 action — Question#index, in which if there is a parameter user_id is returned questions to the user, and if not, all questions.
example 1 — Question#index
example 2 — Anser#index
example 3 — it is logical to make a separate controller, such as Subscription method create
example 4 — Question#create
example 5 — Questions#update
1.2 for each case your method, ie:
example 1 — Question#questions_by_user,
example 2 — Answer#answers_by_user_and_question,
example 3 — User#subscribe_to_question (?),
example 4 — Question#create_for_user,
example 5 — Question#update_ (?).
If you use this approach, how do you choose names for actions? You can write Question#questions_by_user, Question#questions_by_user_id, Question#by_user, Question#all_by_user
2. many inspectors and only the basic steps, i.e.:
example 1 — User::Question#index (app/controllers/users/questions_controller.rb),
example 2 — User::Question::Answer#index
example 3 — Subscription#create (in my opinion here is also appropriate to create a separate controller)
example 4 — User::Question#create
example 5 — User::Question#update

If you use another approach, please share.
by | 21 views

2 Answers

0 like 0 dislike
For each model in the controller. Accordingly, ranting as:
resources :users do resources :questions do resources :answers end resources :subscriptions end 

example 1 — questions#index
example 2 — answers#index
example 3 — subscriptions#create
example 4 — questions#create
example 5 — questions#update
And it is not necessary to put all the controllers in the User, questions and answers are not part of the user.
0 like 0 dislike
resources :users do resources :questions do member do get 'answers' end end member do end end 

Related questions

0 like 0 dislike
6 answers
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
0 like 0 dislike
5 answers
110,608 questions
257,187 answers
40,796 users