私は自分のブログで簡単な投票システムを作成していますが、これにはいくつか問題があります。Rails は初めてです。
これは私のコメントです.rb
include Mongoid::Document
field :name, type: String
field :body, type: String
field :abusive, type: Boolean, default: false
validates_presence_of :name, :body
embedded_in :post, :inverse_of => :comments
belongs_to :user
has_many :votes, :as => :votable,:dependent => :destroy
end
そして私の投票.rb
class Vote
include Mongoid::Document
include Mongoid::Timestamps
field :vote_up
field :vote_down
belongs_to :user
belongs_to :comment
end
私のコメントコントローラーには、このようなものがあります
def vote_up
@post = Post.find(params[:post_id])
comment = @post.comments.find(params[:id])
if @comment
comment.votes.exists?(:user_id => current_user.id)
@notice2 = 'You already voted'
else
@vote_up = comment.votes.create(:user_id => current_user.id, :votable_id => 1)
redirect_to @post, :method => :post
end
end
def vote_down
@post = Post.find(params[:post_id])
comment = @post.comments.find(params[:id])
negative_count = comment.votes.find_all { |c| c.vote_down == true} .count
if negative_count >= 3
#here i would like to add,that comment after 3 negative votes is abusive
end
そして私の見解では
- if @post.comments.size > 0
%h2 Comments
- @post.comments.each do |comment|
%h3= comment.name
%p= comment.body
%i.icon-black.icon-thumbs-up
= link_to "vote_up", vote_up_post_comment_path(@post.id, comment.id), :method => :post
%i.icon-black.icon-thumbs-down
= link_to "vote_down", vote_down_post_comment_path(@post.id, comment.id), :method => :post
これを機能させる方法がわかりません。本当に、ヒントをありがとう。