如何在修改帖子投票系统的基础上创建评论投票系统?

时间:2020-08-16 15:44:11

标签: ruby-on-rails ruby

我在创建reddit克隆时正在学习ruby

到目前为止,我已经为帖子创建了字幕,帖子,评论和投票系统。

剩下的唯一要做的事情就是评论的投票系统和业力系统。

我基本上是尝试复制我的投票系统以发布帖子并将其缩放为评论,但是在遇到每个步骤的错误后,我决定在这里询问如何修改现有代码。

votes_controller:

(您可以在此处看到我如何尝试修改我的代码,它已被注释掉)

class VotesController < ApplicationController

    def create
        
        post_id = params[:post_id]
        vote=Vote.new
        vote.post_id = params[:post_id]
        vote.upvote = params[:upvote]
        vote.account_id = current_account.id 
        #Existing vote check
        existing_vote = Vote.where(account_id: current_account.id, post_id: post_id)
        @new_vote = existing_vote.size<1
        respond_to do |format|
          format.js{
            if existing_vote.size > 0
                existing_vote.first.destroy 
            else  
                if vote.save 
                    @success=true 
                else
                    @success = false
                end
                
                
            end    
            @post = Post.find(post_id)
            @is_upvote = params[:upvote]
            render "votes/create"
          }
        end
    end
    

    # comment_id = params[:comment_id]
    #     vote=Vote.new#(vote_params)
    #     vote.comment_id = params[:comment_id]
    #     vote.upvote = params[:upvote]
    #     vote.account_id = current_account.id 
    #     #Existing vote check
    #     existing_vote_comment = Vote.where(account_id: current_account.id, comment_id: comment_id)
    #     @new_vote_comment = existing_vote.size<1
    #     respond_to do |format|
    #       format.js{
    #         if existing_vote_comment.size > 0
    #             existing_vote_comment.first.destroy 
    #         else  
    #             if vote.save 
    #                 @success=true 
    #             else
    #                 @success = false
    #             end
                
                
    #         end    
    #         @comment = Comment.find(comment_id)
    #         @is_upvote_comment = params[:upvote]
    #         render "votes/create"
    #       }
    #     end
    # end
    private
    def vote_params
        params.require(:vote).permit(:upvote, :post_id)
     #   params.require(:vote).permit(:upvote, :comment_id)
    end
end

vote.js:

$(function() {

    console.log('jquery loaded ');


    $(".vote").on("click", ".upvote, .downvote", function() {
        var post_id = $(this).parent().data("id"),

            is_upvote = $(this).hasClass("upvote");


        console.log("upvote for post " + post_id);
        console.log("is an upvote post" + is_upvote);

        $.ajax({
            url: "/post/vote",
            method: "POST",
            data: { post_id: post_id, upvote: is_upvote },
            success: function() {
                console.log("success..")
            }
        });




    });

});

vote.rb:

class Vote < ApplicationRecord

    belongs_to :account
    belongs_to :post


    validates_uniqueness_of :account_id, scope: :post_id

    after_create :increment_vote
    after_destroy :decrement_vote


    private

    def increment_vote
        field = self.upvote ? :upvotes : :downvotes
        Post.find(self.post_id).increment(field).save
    end

    def decrement_vote
        field = self.upvote ? :upvotes : :downvotes
        Post.find(self.post_id).decrement(field).save

    end

end

vote_comment.rb(基本修改为表决的版本):

class VoteComment<ApplicationRecord



    belongs_to :account
    belongs_to :comment


    validates_uniqueness_of :account_id, scope: :comment_id

    after_create :increment_vote
    after_destroy :decrement_vote


    private

    def increment_vote
        field = self.upvote ? :upvotes : :downvotes
        Comment.find(self.comment_id).increment(field).save
    end

    def decrement_vote
        field = self.upvote ? :upvotes : :downvotes
        Comment.find(self.comment_id).decrement(field).save

    end

end
_comment部分的

部分,用于显示和发送投票:

<div id="vote-actions-<%=comment.id%>" class="vote" data-id="<%=comment.id%>">
  <div class ="fa fa-arrow-up upvote <%= is_upvoted comment%>" ></div>
  <span class="font-weight-bold score"><%=@comment.score%></span>
  <div class ="fa fa-arrow-down downvote <%= is_downvoted comment%> "></div>

create.js.erb:

$("#vote-actions-<%=@post.id%> .score").text("<%=@post.score%>");

$("#vote-actions-<%=@post.id%> .downvote, #vote-actions-<%=@post.id%> .upvote ").removeClass("active");


<% if @new_vote %>
    


<% if @is_upvote == "true" %>
$("#vote-actions-<%=@post.id%> .upvote").addClass("active");
<% else %>
$("#vote-actions-<%=@post.id%> .downvote").addClass("active");

<% end %>

<% end %>

部分架构文件:

  create_table "votes", force: :cascade do |t|
    t.bigint "account_id"
    t.bigint "post_id"
    t.boolean "upvote"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["account_id"], name: "index_votes_on_account_id"
    t.index ["post_id"], name: "index_votes_on_post_id"
  end

  create_table "votescomments", force: :cascade do |t|
    t.bigint "account_id"
    t.bigint "comment_id"
    t.boolean "upvote"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["account_id"], name: "index_votescomments_on_account_id"
    t.index ["comment_id"], name: "index_votescomments_on_comment_id"
  end

I have a feeling I forgot something so here is a git repo

0 个答案:

没有答案