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 class CandidatesController < ApplicationController
before_action :set_candidate, only: [:show, :edit, :update, :destroy] before_action :set_candidate, only: [:show, :edit, :update, :destroy]
def index def index
@candidates = Candidate.all @candidates = Candidate.all.group_by(&:position_id)
end end
def new def new
...@@ -19,6 +19,15 @@ class CandidatesController < ApplicationController ...@@ -19,6 +19,15 @@ class CandidatesController < ApplicationController
end end
def show 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 end
def edit def edit
......
...@@ -20,6 +20,9 @@ class PositionsController < ApplicationController ...@@ -20,6 +20,9 @@ class PositionsController < ApplicationController
end end
def show def show
#lastfunctionleft
@position = Position.find(params[:id])
end end
def edit def edit
......
...@@ -6,7 +6,7 @@ class VotesController < ApplicationController ...@@ -6,7 +6,7 @@ class VotesController < ApplicationController
end end
def vote 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." redirect_to root_path, notice: "You have already exceeded your number of votes."
else else
@vote = Vote.new @vote = Vote.new
......
<div class = "bottomspace">
<h1>Candidates</h1> <h1>Candidates</h1>
<% @candidates.each do |position_id, candidates| %>
<%= Position.find(position_id).name %>
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<th>ID</th>
<th>First Name</th> <th>First Name</th>
<th>Last Name</th> <th>Last Name</th>
<th>Slogan</th> <th>Slogan</th>
<th>Position</th>
<th colspan="3"></th> <th colspan="3"></th>
</thead> </thead>
<tbody> <tbody>
<% @candidates.each do |candidate| %> <% candidates.each do |candidate| %>
<tr> <tr>
<td><%= candidate.id %></td>
<td><%= candidate.first_name %></td> <td><%= candidate.first_name %></td>
<td><%= candidate.last_name %></td> <td><%= candidate.last_name %></td>
<td><%= candidate.slogan %></td> <td><%= candidate.slogan %></td>
<td><%= candidate.position.name %></td>
<td> <td>
<%= link_to "Show", candidate_path(candidate), class: "btn btn-info" %> <%= link_to "Show", candidate_path(candidate), class: "btn btn-info" %>
</td> </td>
...@@ -27,8 +28,11 @@ ...@@ -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" %> <%= link_to "Delete", candidate_path(candidate), method: :delete, data: { confirm: "Are you sure you want to delete?" }, class: "btn btn-danger" %>
</td> </td>
</tr> </tr>
<% end %>
</tbody> </tbody>
<% end %>
</table> </table>
<br><br>
<% end %>
<%= link_to "Add candidate", new_candidate_path, class: "btn btn-primary" %> <%= link_to "Add candidate", new_candidate_path, class: "btn btn-primary" %>
<div class = "bottomspace">
\ No newline at end of file
...@@ -25,4 +25,19 @@ ...@@ -25,4 +25,19 @@
<%= @candidate.position.name %> <%= @candidate.position.name %>
</p> </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" %> <%= link_to "Back", candidates_path, class: "btn btn-default" %>
\ No newline at end of file
...@@ -20,24 +20,22 @@ ...@@ -20,24 +20,22 @@
<a class="navbar-brand" href="/">Election System</a> <a class="navbar-brand" href="/">Election System</a>
</div> </div>
<div class="collapse navbar-collapse"> <div class="collapse navbar-collapse">
<% if user_signed_in? %>
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li> <li>
<%= link_to "Available Candidates", candidates_list_path %> <%= link_to "Available Candidates", candidates_list_path %>
</li> </li>
<li>
<%= link_to "Votes Record", vote_history_path %>
</li>
<li> <li>
<%= link_to "About", about_path %> <%= link_to "About", about_path %>
</li> </li>
</ul> </ul>
<% end %>
<p class="navbar-text pull-right"> <p class="navbar-text pull-right">
<% if user_signed_in? %> <% 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 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> |
<%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %> <%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %>
<% else %> <% else %>
......
<h1>Positions</h1> <h1>Candidates for <%= @position.name %></h1>
<ol>
<p> <% @position.candidates.each do |v| %>
<strong>Position: </strong> <li><%= v.first_name %>&nbsp<%= v.last_name %></li>
<%= @position.id %> <% end %>
</p> </ol>
<p>
<strong>Name: </strong>
<%= @position.name %>
</p>
<br><br>
<%= link_to "Back", positions_path, class: "btn btn-default" %> <%= link_to "Back", positions_path, class: "btn btn-default" %>
<div class="bottomspace"> <div class="bottomspace">
<h1>Vote Ballot</h1> <h1>Vote Ballot</h1>
<br>
<% @candidates.each do |position_id, candidates| %> <% @candidates.each do |position_id, candidates| %>
<%= simple_form_for(@vote, url: confirm_vote_path, method: :post) do |o| %> <%= 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 --> <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.association :candidate, :collection => candidates %></li>
<li><%= o.input :comments %></li> <li><%= o.input :comments %></li>
</ul> </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 %> <% end %>
<%= link_to "Back", candidates_list_path, class: "btn btn-default" %>
<% end %> <% end %>
</div> </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"> <table class="table table-striped">
<thead> <thead>
<th>Voted Candidate</th> <th><center>Voted Candidate</th>
<th>Position </th> <th><center>Position </th>
<th>Voted On</th> <th><center>Voted On</th>
</thead> </thead>
<tbody> <tbody>
<% @votes.each do |o| %> <% @votes.each do |o| %>
<tr> <tr>
<td><%= o.candidate.first_name %>&nbsp<%= o.candidate.last_name %></td> <td><%= o.candidate.last_name %>,&nbsp;<%= o.candidate.first_name %></td>
<td><%= o.candidate.position.name %></td> <td><center><%= o.candidate.position.name %></td></center>
<td><%= o.created_at %></td> <td><center><%= o.created_at %></td></center>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
\ No newline at end of file </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