Question

I am new to using inherited resources and want to use it for polymorphic nested comments. I have several objects that will be commentable (articles, galleries, etc.) and comments can also be nested. I'm using a combination of awesome_nested_set (parent_id, lft, rgt) with my Comment model having polymorphic commentable columns.

The controller needs to receive an AJAX request (only) for the create action and perform as below:

Posting to /articles/12/comments/34 creates a comment with commentable equal to @article (12) and parent equal to @comment (34)

/articles/12/comments/34

Posting to /gallery/12/comments/34 creates a comment with commentable equal to @gallery (12) and parent equal to @comment (34)

I'm a bit stuck on where to begin. Is this a good use case for inherited resources?


class CommentsController < InheritedResources::Base 
  respond_to :js, :only => :create 
  belongs_to :article, :cheat, :gallery, :video, :polymorphic => true 
do 
    belongs_to :comments 
  end 
  def create 
    create! do |format| 
       # How in here do I build a comment so that it handles 
polymorphism? 
       @comment.children.create(:commentable => @article or @cheat or 
@something_generic?) 
    end 
  end 
end 
Was it helpful?

Solution

Nested comments are difficult. Rails resources don't provide in passing an id to the create action, so I don't think you can properly POST to the create action this way. You might need to add comment to the belong_to options, so you can post to /comments/33/comments. I don't know if InheritedResources support this. If it does, object creation should be automatic. You can reference stuff like parent_url too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top