Using controller for association to render different partials depending on the belongs_to object type
My question was if Post has_many Comments, and I just want to update a comment, should I do that in the Post, or Comments controllers. I just watched the Railscasts on Polymorphic Associations and saw that he does it in the comments controller.
But what if I need to render a different partial depending on which commentable type it was called by? In other words, if a Post comment is being added, then render partial _post_comment. If a Video comment is being added, then render partial _vi开发者_开发知识库deo_comment, etc.
You should keep the logic in the comments controller.
You can render the partial depending on the linked object doing:
case @comment.commentable
when Post
render ...
when Video
render ...
else
...
end
精彩评论