Commit 23b4d9a4 authored by Anferny Vanta's avatar Anferny Vanta

Full functionality;deleting of positions allowed but candidates will be...

Full functionality;deleting of positions allowed but candidates will be missing an attribute causing an error
parent ad531e93
class CandidatesController < ApplicationController
before_action :set_candidate, only: [:show, :edit, :update, :destroy]
def index
@candidates = Candidate.all
@candidates = Candidate.all.group_by(&:position_id)
end
def new
......@@ -19,6 +19,15 @@ class CandidatesController < ApplicationController
end
def show
@male = 0
@female = 0
Vote.where(candidate_id: params[:id]).each do |v|
if User.find(v.user_id).gender == "Male"
@male += 1
else
@female += 1
end
end
end
def edit
......
......@@ -20,6 +20,9 @@ class PositionsController < ApplicationController
end
def show
#lastfunctionleft
@position = Position.find(params[:id])
end
def edit
......
......@@ -6,7 +6,7 @@ class VotesController < ApplicationController
end
def vote
if current_user.votes.count > Position.count
if current_user.votes.count >= Position.count
redirect_to root_path, notice: "You have already exceeded your number of votes."
else
@vote = Vote.new
......
<div class = "bottomspace">
<h1>Candidates</h1>
<% @candidates.each do |position_id, candidates| %>
<%= Position.find(position_id).name %>
<table class="table table-striped">
<thead>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Slogan</th>
<th>Position</th>
<th colspan="3"></th>
</thead>
<tbody>
<% @candidates.each do |candidate| %>
<% candidates.each do |candidate| %>
<tr>
<td><%= candidate.id %></td>
<td><%= candidate.first_name %></td>
<td><%= candidate.last_name %></td>
<td><%= candidate.slogan %></td>
<td><%= candidate.position.name %></td>
<td>
<%= link_to "Show", candidate_path(candidate), class: "btn btn-info" %>
</td>
......@@ -27,8 +28,11 @@
<%= link_to "Delete", candidate_path(candidate), method: :delete, data: { confirm: "Are you sure you want to delete?" }, class: "btn btn-danger" %>
</td>
</tr>
<% end %>
</tbody>
<% end %>
</table>
<br><br>
<% end %>
<%= link_to "Add candidate", new_candidate_path, class: "btn btn-primary" %>
<div class = "bottomspace">
\ No newline at end of file
......@@ -25,4 +25,19 @@
<%= @candidate.position.name %>
</p>
<p>
<strong>Total Votes: </strong>
<%= @candidate.votes.count %>
</p>
<p>
<strong>Votes from Male(s): </strong>
<%= @male %>
</p>
<p>
<strong>Votes from Females(s): </strong>
<%= @female %>
</p>
<%= link_to "Back", candidates_path, class: "btn btn-default" %>
\ No newline at end of file
......@@ -20,24 +20,22 @@
<a class="navbar-brand" href="/">Election System</a>
</div>
<div class="collapse navbar-collapse">
<% if user_signed_in? %>
<ul class="nav navbar-nav">
<li>
<%= link_to "Available Candidates", candidates_list_path %>
</li>
<li>
<%= link_to "Votes Record", vote_history_path %>
</li>
<li>
<%= link_to "About", about_path %>
</li>
</ul>
<% end %>
<p class="navbar-text pull-right">
<% if user_signed_in? %>
Logged in as <strong><%= current_user.email %></strong>.
Logged in as <strong><%= link_to "#{current_user.first_name} #{current_user.last_name}", vote_history_path %></strong>
&nbsp
<%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> |
<%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %>
<% else %>
......
<h1>Positions</h1>
<p>
<strong>Position: </strong>
<%= @position.id %>
</p>
<p>
<strong>Name: </strong>
<%= @position.name %>
</p>
<h1>Candidates for <%= @position.name %></h1>
<ol>
<% @position.candidates.each do |v| %>
<li><%= v.first_name %>&nbsp<%= v.last_name %></li>
<% end %>
</ol>
<br><br>
<%= link_to "Back", positions_path, class: "btn btn-default" %>
<div class="bottomspace">
<h1>Vote Ballot</h1>
<br>
<% @candidates.each do |position_id, candidates| %>
<%= simple_form_for(@vote, url: confirm_vote_path, method: :post) do |o| %>
<%= Position.find(position_id).name %>
<strong><u><%= Position.find(position_id).name %></u></strong>
<ul> <!--:candidate refers to all records, collection will filter and only show the candidates associated with a certain position -->
<li><%= o.association :candidate, :collection => candidates %></li>
<li><%= o.input :comments %></li>
</ul>
<%= o.button :submit, "Vote", class: "btn btn-primary" %>
<% voted = false %>
<% Vote.where(user_id: current_user.id).each do |v| %>
<% if Candidate.find(v.candidate_id).position_id == position_id %>
<% voted = true %>
<% end %>
<% end %>
<% if voted == false %>
<center><%= o.button :submit, "Vote", class: "btn btn-primary" %> <%= link_to "Back", candidates_list_path, class: "btn btn-default" %></center>
<% else %>
<center><%= link_to "You have already voted for this position", vote_path, disabled: true, class: "btn btn-danger" %> <%= link_to "Back", candidates_list_path, class: "btn btn-default" %></center>
<% end %>
<br>
<% end %>
<%= link_to "Back", candidates_list_path, class: "btn btn-default" %>
<% end %>
</div>
<h1>Vote Record</h1>
<div class="jumbotron">
<h1><%= current_user.first_name %>&nbsp;<%= current_user.last_name %></h1>
<p><strong>Birthday: <%= current_user.birthday %></p></strong>
<p><strong>E-mail: <%= current_user.email %></strong></p>
<br>
<table class="table table-striped">
<thead>
<th>Voted Candidate</th>
<th>Position </th>
<th>Voted On</th>
<th><center>Voted Candidate</th>
<th><center>Position </th>
<th><center>Voted On</th>
</thead>
<tbody>
<% @votes.each do |o| %>
<tr>
<td><%= o.candidate.first_name %>&nbsp<%= o.candidate.last_name %></td>
<td><%= o.candidate.position.name %></td>
<td><%= o.created_at %></td>
<td><%= o.candidate.last_name %>,&nbsp;<%= o.candidate.first_name %></td>
<td><center><%= o.candidate.position.name %></td></center>
<td><center><%= o.created_at %></td></center>
</tr>
<% end %>
</tbody>
</table>
\ No newline at end of file
</table>
</p>
</div>
\ 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