I have implemented like and dislike functions for my posts already and they work just fine. In this phase I'm trying to provide some admin powers and privileges for my app. One of them is having the admin being able to reset votes for a post. A simple button that when clicked, the vote count will be reset to zero or better to say all votes get destroyed; that's it.
The app is a pinterest clone. This is 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