I am working on the implementation of polymorphic comments (they can be applied to almost any user-generated content on the site and are not limited to instances Article. When creating a comment, I need to determine which one commentableit belongs to. Most of the letters that I found on this subject suggest that I I use the template specified in the find_commentablecode below, but this approach does not seem very elegant to me - it would seem that there should be a simple way to unambiguously indicate the commentablecreated new comment, without going through the set paramsand without matching with Is there a better way?
In other words, there is a better way to access the object commentablefrom the controller commentin the context of commentableβ commentassociation? It still works with a method create, where we still do not have an object @commentto work with?
My models are configured as follows:
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable, dependent: :destroy
end
class CommentsController < ApplicationController
def create
@commentable = find_commentable
@comment = @commentable.comments.build(comment_params)
if @comment.save
redirect_to :back
else
render :action => 'new'
end
end
def index
@commentable = find_commentable
@comments = @commentable.comments
end
private
def comment_params
params.require(:comment).permit(:body)
end
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
end
end
Thank!
source
share