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
......@@ -19,7 +19,7 @@ class PositionsController < ApplicationController
end
end
def edit
@position = Position.find(params[:id])
@position = Position.find(params[:id])
end
def update
@position = Position.find(params[:id])
......@@ -31,9 +31,9 @@ class PositionsController < ApplicationController
end
end
def show
@position = Position.find(params[:id])
@candidates = Candidate.all
render "admin/positions/show.html.erb"
@position = Position.find(params[:id])
@candidates = Candidate.all
render "admin/positions/show.html.erb"
end
def destroy
@position = Position.find(params[:id])
......@@ -47,10 +47,10 @@ class PositionsController < ApplicationController
@position.destroy
@candidates.destroy_all
redirect_to admin_positions_path
end
end
def position_params
params.require(:position).permit!
end
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