これが私がacts_as_commentablegemでもそれをやった方法です。ですから、これはあなたがコメントしているどんなオブジェクトでもうまくいくはずだと思います。
私の_comment.html.erbビューで
<%= link_to "Upvote", {:controller =>"comments", :action => "upvote", :id => comment.id}, :class => 'btn', method: :put %>
<%= link_to "Downvote", {:controller =>"comments", :action => "downvote", :id => comment.id}, :class => 'btn', method: :put %>
私のroutes.rbファイルで
put '/comments/:id/:action' => 'comments#upvote'
put '/comments/:id/:action' => 'comments#downvote'
それから私のコメントコントローラーで
class CommentsController < ApplicationController
before_filter :load_commentable
before_filter :find_comment, :only => [:upvote, :downvote]
def upvote
current_user.upvotes @comment
redirect_to(@comment.commentable)
end
def downvote
@comment.downvote_from current_user
redirect_to(@comment.commentable)
end
private
def load_commentable
resource, id = request.path.split('/')[1, 2]
@commentable = resource.singularize.classify.constantize.find(id)
end
def find_comment
@comment = Comment.find(@commentable.id)
end
end
beforeフィルターはより多様性を可能にするので、これをコメント可能なオブジェクトに追加できます。私はたまたまお祭りでしたが、写真など何でもできました。詳細については、acts_as_commentableのドキュメントとpolymorphicrailscastを確認してください。これは私の最初の投稿なので、これがひどいコードである場合は、教えてください。