candidates_controller.rb 1.19 KB
Newer Older
April Guian's avatar
April Guian committed
1 2 3 4 5 6 7
module Admin
  class CandidatesController < ApplicationController
    def index
      @positions = Position.all
      render "admin/candidates/index.html.erb"
    end

Galen Evilla's avatar
Galen Evilla committed
8 9 10 11 12 13
    def new
      @candidate = Candidate.new
      render "admin/candidates/new.html.erb"
    end

    def edit
April Guian's avatar
April Guian committed
14
      @candidate = Candidate.find(params[:id])
Galen Evilla's avatar
Galen Evilla committed
15 16 17 18 19 20 21 22 23 24
    end

    def update
      @candidate = Candidate.find(params[:id])

      if @candidate.update(candidate_params())
        redirect_to admin_candidates_path(@candidate.id)
      else
        render "admin/candidates/edit.html.erb"
      end
April Guian's avatar
April Guian committed
25 26 27 28
    end

    def destroy
      @candidate = Candidate.find(params[:id])
Galen Evilla's avatar
Galen Evilla committed
29 30 31 32
      
      @candidate.votes.each do |v|
        v.destroy!
      end
April Guian's avatar
April Guian committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46
      @candidate.destroy!
      redirect_to admin_candidates_path
    end

    def create
      @candidate = Candidate.new(candidate_params())

      if @candidate.save
        redirect_to admin_candidate_path(@candidate.id)
      else
        render "admin/candidates/new.html.erb"
      end
    end

Galen Evilla's avatar
Galen Evilla committed
47
    def show
April Guian's avatar
April Guian committed
48
      @candidate = Candidate.find(params[:id])
Galen Evilla's avatar
Galen Evilla committed
49
      render "admin/candidates/show.html.erb"
April Guian's avatar
April Guian committed
50 51 52 53 54 55 56 57
    end


    def candidate_params
      params.require(:candidate).permit!
    end
  end
end