Rails - built-in list of polymorphic comments + add comment form - example?

Hey there. Work on my first Rails application.

I searched all around - read a bunch of tutorials, articles and forum posts, looked at some screencasts, and I found some examples that come close to what I'm trying to do (especially http://railscasts.com/episodes/154-polymorphic- association and ep 196 on nested model forms), but not really.

I have two models (Podcast and BlogPost) that should be commented on, and I have a comment model that is polymorphically related to both. The railscasts above had a very similar example (ep 154), but Ryan used a full set of nested routes, so there were special templates for adding and editing comments. I want to show a list of comments right on the Podcast or BlogPost page along with the Add Comment form below. I do not need a separate template / add route, and I do not need the ability to edit, delete only.

This is a fairly common design on the Internet, but I cannot find a Rails example specifically for this template. Here is my real understanding:

I need routes to create and delete actions, of course, but there are no templates associated with them. I also assume that the right approach is to create a partial part that can be included at the bottom of the show Podcast and BlogPost template. The logical name for the partial seems to me something like _comments.html.haml. I know that this is a common convention that the object passed to the partial be named after the template, but calling the comments of the object “seems” does not match my use case, since what I really need to pass is a comment (Podcast or BlogPost) . So, I assume that I would use the locals parameter to partially invoke rendering? (: commentable => @podcast).

Inside the partial, I could call commentable.comments to get a collection of comments, do it with a second partial (this time with the usual use case that called partial _comment.html.haml), then create a form that submits to what? REST-wise, this should be POST for the collection, which will be / podcast | blogpost /: id / comments, and I think the helper for this is podcast_comments_path (podcast), if it was a podcast, I’m not sure what to do, though, since I use polymorphic comments. This will trigger the Comment.create action, which will then have to redirect back to the podcast | blogpost path / podcast | blogpost /: id.

All this is a little overwhelming, so I really hoped to find a screencast or example that specifically implements this project.

+4
source share
1 answer

I figured it out, implemented it, tested it, then finally wrote a tutorial explaining how to do it for others:

https://github.com/hcatlin/make_resourceful/wiki/embedded-nested-polymorphic-comments

+15
source

Source: https://habr.com/ru/post/1299718/


All Articles