rails Strukturierung von Routen / Controllern / Views für verschachtelte Ressourcen
Ich habe Schienen App mit der folgenden Struktur:
user has_many posts
post has_many post_comments
post_comment has_many comment_replies
Ich plane, die folgenden Routen zu verwenden, um ein tiefes Verschachteln zu vermeiden.
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
Dies ergibt die folgende Reglerstruktur
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
Mein Problem ist mit der Strukturierung der Ordner.
Für Controller: Das bedeutet, dass ich dasposts_controller
im Hauptordner und daspost_comments_controller
gehört inposts
Mappe. Bis jetzt ist es klar. Aber fürcomment_replies
Ich sollte es in das @ setzcomment_replies
-Ordner, der vollständig von dem @ -Ordner getrennt ipost_comments_controller
kann gefunden werden
Für Ansichten: Ich habe eincreate.js.erb
zumposts
im Ordner 'post'. Ich habe eincreate.js.erb
zumpost_comment
in demposts/post_comments
Mappe. Entsprechend meinen Routen sollte ich das @ setzcreate.js.erb
zumcomment_replies
in daspost_comments/comment_replies folder
. Dies scheint aber wieder uninteressant zu sein, genau wie das Controller-Beispiel.
Was ist in diesem Fall die Rails Convention / Common Solution? Btw. Ich möchte Kommentare oder Antworten nicht getrennt von ihrem Beitrag anzeigen.
AKTUALISIERE
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 controller
def create
@post = Post.find(params[:id])
@post_comment = @post.post_comments.build(post_comment_params)
@post_comment.save!
end