我已经为我的帖子实现了喜欢和不喜欢的功能,它们工作得很好。在这个阶段,我试图为我的应用程序提供一些管理权力和特权。其中之一是让管理员能够重置帖子的投票。一个简单的按钮,点击后,投票计数将重置为零或更好地表示所有投票都被销毁;而已。
该应用程序是一个 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