我已經爲我的帖子實現了喜歡和不喜歡的功能,並且它們工作得很好。在這個階段,我試圖爲我的應用程序提供一些管理權限和權限。其中之一是讓管理員可以重新設置帖子的投票。一個簡單的按鈕,當點擊時,投票計數將被重置爲零或更好地說全部投票被破壞;而已。如何在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
核心功能很簡單 - 通過活動記錄查詢找到帖子,然後調用'destroy_all'。除此之外,這取決於你的應用程序如何專門放在一起 - 如果不顯示你的嘗試到目前爲止不可能知道。 –