I have a rails application with the following structure:
user has_many posts post has_many post_comments post_comment has_many comment_replies
I plan to use the following routes to avoid deep nesting.
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
This gives the following controller structure
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
My problem is stacking folders.
For controllers: this means that I put posts_controller in the main folder, and post_comments_controller goes to the posts folder. This is still clear. But for comment_replies I have to put it in the comment_replies folder, which is completely separate from where post_comments_controller can be found.
For views: I have create.js.erb for posts in the "post" folder. I have create.js.erb for post_comment in the posts/post_comments . For my routes, I have to put create.js.erb for comment_replies in the post_comments/comment_replies folder . But this seems to be incompatible again, as an example of a controller.
What is rails solution / general solution in this case? Btw. I do not want to show comments or answers separately from their post.
UPDATE
posts_controller
def index .... @post_comments = @post.post_comments
post_comments controller
def create @post = Post.find(params[:id]) @post_comment = @post.post_comments.build(post_comment_params) @post_comment.save! end
source share