开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜