Commit 655ba07d authored by Bianca Tarun's avatar Bianca Tarun

added candidates_controller

parent 1cf66a18
module Admin
class CandidatesController < ApplicationController
before_action :authenticate_user!
def index
@candidates = Candidate.all
@positions = Postion.all
render "admin/candidates/index.html.erb"
end
def new
@position = Candidate.new
render "admin/candidates/new.html.erb"
end
def create
@candidate = Candidate.new(candidate_params())
if @position.save
redirect_to admin_candidate_path(@candidate.id)
else
render "admin/candidates/new.html.erb"
end
end
def update
@candidate = Candidate.find(params[:id])
if @candidate.update(candidate_params())
redirect_to admin_candidate_path(@candidate.id)
else
render "admin/candidate/edit.html.erb"
end
end
def show
@candidate = Candidate.find(params[:id])
@votes = Vote.where(candidate_id: (@candidate.id))
@votes_male = Array.new
@votes_female = Array.new
@votes.each do |vote|
if vote.user.gender == "Male"
@votes_male.push(vote)
else
@votes_female.push(vote)
end
end
end
def destroy
@candidate = Candidate.find(params[:id])
@votes = Vote.where(candidate_id: (@candidate.id))
@votes.destroy_all
@candidate.destroy
redirect_to admin_candidates_path
end
def candidate_params
params.require(:candidate).permit!
end
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment