0

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

This is how the app looks

Armin
  • 1
  • 2
  • the core functionality is simple - you find the posts via an active record query, then call `destroy_all`. Beyond that it depends on how your app is specifically put together - impossible to know without showing your attempt thus far. – max pleaner Aug 23 '16 at 20:05

1 Answers1

0

I don't know your code layout but something I would do would roughly be having the button pass in the post_id params to the controller,

post = Post.find(permitted_params[:post][:id])
Vote.where("post_id = ?", post.id).destroy_all
Nick Schwaderer
  • 1,030
  • 1
  • 8
  • 21
  • Sorry I should've provided my codes earlier. I'm noob with RoR atm. I just added my pins_controller. It'd be great if you could walk me through it with both the controller and the view. Thanks – Armin Aug 24 '16 at 13:48