rails структурирование маршрутов / контроллера / представлений для вложенных ресурсов
У меня есть приложение рельсы со следующей структурой:
user has_many posts
post has_many post_comments
post_comment has_many comment_replies
Я планирую использовать следующие маршруты, чтобы избежать глубокого вложения.
resources :posts do
resources :post_comments, module: :posts
end
resources :comments do
resources :comment_replies, module: :post_comments #is this module a good choice?
end
Это дает следующую структуру контроллера
post_comments GET /posts/:post_id/comments(.:format) posts/comments#index
POST /posts/:post_id/comments(.:format) posts/comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) posts/comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) posts/comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) posts/comments#show
PATCH /posts/:post_id/comments/:id(.:format) posts/comments#update
PUT /posts/:post_id/comments/:id(.:format) posts/comments#update
DELETE /posts/:post_id/comments/:id(.:format) posts/comments#destroy
comment_repiles GET /comments/:comment_id/repiles(.:format) comments/repiles#index
POST /comments/:comment_id/repiles(.:format) comments/repiles#create
new_comment_repile GET /comments/:comment_id/repiles/new(.:format) comments/repiles#new
edit_comment_repile GET /comments/:comment_id/repiles/:id/edit(.:format) comments/repiles#edit
comment_repile GET /comments/:comment_id/repiles/:id(.:format) comments/repiles#show
PATCH /comments/:comment_id/repiles/:id(.:format) comments/repiles#update
PUT /comments/:comment_id/repiles/:id(.:format) comments/repiles#update
DELETE /comments/:comment_id/repiles/:id(.:format) comments/repiles#destroy
Моя проблема с структурированием папок.
Для контроллеров: это означает, что я поставилposts_controller
в главной папке иpost_comments_controller
переходит вposts
папка. До сих пор это понятно. Но дляcomment_replies
Я должен положить это вcomment_replies
папка, которая полностью отделена от гдеpost_comments_controller
может быть найден.
Для просмотра: у меня естьcreate.js.erb
заposts
в папке «пост». у меня естьcreate.js.erb
заpost_comment
вposts/post_comments
папка. По моим маршрутам я должен поставитьcreate.js.erb
заcomment_replies
вpost_comments/comment_replies folder
, Но это, кажется, снова нелогично, как в примере с контроллером.
Что такое соглашение о рельсах / общее решение в этом случае? Btw. Я не хочу показывать комментарии или ответы отдельно от их поста.
ОБНОВИТЬ
posts_controller
def index
....
@post_comments = @post.post_comments #showing all comments
@post_comment = PostComment.new #new comment via js
respond_to do |format|
format.js
end
end
контроллер post_comments
def create
@post = Post.find(params[:id])
@post_comment = @post.post_comments.build(post_comment_params)
@post_comment.save!
end