3

Hey, all. Working on my first Rails app.

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

I have two models (Podcast and BlogPost) that need to be commentable, 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 specific templates for adding and editing comments. What I want to do is show the list of comments right on the Podcast or BlogPost page, along with an Add Comment form at the bottom. I don't need a separate add template/route, and I don't need the ability to edit, only delete.

This is a pretty common design on the web, but I can't find a Rails example specifically about this pattern. Here's my current understanding:

I need routes for the create and delete actions, of course, but there are no templates associated with those. I'm also guessing that the right approach is to create a partial that can be included at the bottom of both the Podcast and BlogPost show template. The logical name for the partial seems to me to be something like _comments.html.haml. I know it's a common convention to have the object passed to the partial be named after the template, but calling the object 'comments' seems to not match my use case, since what I really need to pass is the commentable object (Podcast or BlogPost). So, I guess I'd use the locals option for the render partial call? (:commentable => @podcast).

Inside the partial, I could call commentable.comments to get the comments collection, render that with a second partial (this time with the conventional use case, calling the partial _comment.html.haml), then create a form that submits to... what? REST-wise, it should be a POST to the collection, which would be /podcast|blogpost/:id/comments, and I think the helper for that is podcast_comments_path(podcast) if it were a podcast - not sure what to do though, since I'm using polymorphic comments. That would trigger the Comment.create action, which would then need to redirect back to the podcast|blogpost path /podcast|blogpost/:id.

It's all a bit overwhelming, which is why I was really hoping to find a screencast or example that specifically implements this design.

mat
  • 12,943
  • 5
  • 39
  • 44
odigity
  • 7,568
  • 4
  • 37
  • 51
  • Do you have any source showing what you've tried and where it's breaking down? Your post is kind of overwhelming, so maybe seeing some code will prove helpful. – theIV Jan 29 '10 at 08:05
  • Sorry, I don't, because I'm not sure how to implement it. I can provide supporting code, like model and route definitions, if you think it's relevant. Also, if it's not clear from the description what I'm trying to do, I can try to clarify it better... – odigity Jan 29 '10 at 08:25
  • This should not really have the embedded tag as it is not about programming an embedded system. I do not have enough rep to change the tag. – uɐɪ Jan 29 '10 at 10:16
  • I updated the tags with something more sensible. It's my first question on stackoverflow, wasn't really sure what was appropriate, and I was still mentally in Rails-land. – odigity Jan 29 '10 at 11:34

1 Answers1

14

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

RamC
  • 1,287
  • 1
  • 11
  • 15
odigity
  • 7,568
  • 4
  • 37
  • 51