我在博客上做了一些简单的投票系统。我遇到了一些问题。我是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
我不知道如何做这项工作,真的。感谢你的任何提示。