0

我在我的博客中做了一些简单的投票系统。我对此有一些问题。我是 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

我不知道如何使这项工作,真的。谢谢你的任何提示。

4

0 回答 0