2016-08-23 27 views
0

我已經爲我的帖子實現了喜歡和不喜歡的功能,並且它們工作得很好。在這個階段,我試圖爲我的應用程序提供一些管理權限和權限。其中之一是讓管理員可以重新設置帖子的投票。一個簡單的按鈕,當點擊時,投票計數將被重置爲零或更好地說全部投票被破壞;而已。如何在Ruby on Rails中使用acts_as_votable來銷燬模型的所有選票

該應用程序是一個pinterest克隆。 這是pins_controller

class PinsController < ApplicationController 
before_action :find_pin, only: [:show, :pinner, :edit, :update, :destroy, :upvote, :downvote] 
before_action :authenticate_user!, except: [:index, :show] 

def index 
    @pins = Pin.all.order("created_at DESC") 
end 

def show 
end 

def new 
    @pin = current_user.pins.build 
end 

def create 
    @pin = current_user.pins.build(pin_params) 

    if @pin.save 
     redirect_to @pin, notice: "Successfully created new Pin" 
    else 
     render 'new' 
    end 
end 

def edit 
end 

def update 
    if @pin.update(pin_params) 
     redirect_to @pin, notice: "Pin was successfully updated!" 
    else 
     render 'edit' 
    end 
end 

def destroy 
    @pin.destroy 
    redirect_to root_path 
end 

def upvote 
    @pin.upvote_by current_user 
    redirect_to :back 
end 

def downvote 
    @pin.downvote_by current_user 
    redirect_to :back 
end 

private 

def pin_params 
    params.require(:pin).permit(:title, :description, :image, :extlink) 
end 

def find_pin 
    @pin = Pin.find(params[:id]) 
end 

end 

This is how the app looks

+0

核心功能很簡單 - 通過活動記錄查詢找到帖子,然後調用'destroy_all'。除此之外,這取決於你的應用程序如何專門放在一起 - 如果不顯示你的嘗試到目前爲止不可能知道。 –

回答

0

我不知道你的代碼的佈局,但東西我會做會大致是一種在POST_ID PARAMS到控制器的按鈕通,

post = Post.find(permitted_params[:post][:id]) 
Vote.where("post_id = ?", post.id).destroy_all 
+0

對不起,我應該提供我的代碼。我很喜歡RoR atm。我剛加了我的pins_controller。如果你能夠通過控制器和視圖來完成它,那將是非常棒的。謝謝 – arminmon

相關問題