Commit 4013142a authored by Kristoff Sison's avatar Kristoff Sison
parents 2f546dba ad30be84
class VotesController < ApplicationController class VotesController < ApplicationController
def index def index
@votes = Vote.all @votes = current_user.votes
@candidates = Candidate.all
@positions = Position.all
render "votes/index.html.erb" render "votes/index.html.erb"
end end
def new
@positions = Position.all
@vote = Vote.new
@candidates = []
@all_candidates= Candidate.pluck(:id)
@candidate_voted = current_user.votes.pluck(:candidate_id).uniq
@all_candidates.each do |candidate|
@candidate_voted.each do |voted|
if candidate = voted
@all_candidates.delete(candidate)
end
end
end
@all_candidates.each do |c1|
@candidates.push(Candidate.find(c1))
end
if @candidates.empty?
redirect_to root_path, notice: "You have already voted all candidates"
end
end
def create
@candidate = Candidate.find(vote_params[:candidate_id])
@vote = Vote.new(vote_params)
@vote.user = current_user
if @vote.save
redirect_to root_path
else
render :action =>'show'
end
end
def update
end
def show
@positions = Position.all
@votes = current_user.votes
@candidates = Candidate.all
end
def vote_params
params.require(:vote).permit!
end
end end
\ No newline at end of file
...@@ -4,4 +4,7 @@ class Candidate < ActiveRecord::Base ...@@ -4,4 +4,7 @@ class Candidate < ActiveRecord::Base
validates :slogan, uniqueness: true, presence: true validates :slogan, uniqueness: true, presence: true
belongs_to :position belongs_to :position
has_many :votes has_many :votes
def to_s
"#{first_name} #{last_name}"
end
end end
...@@ -2,4 +2,7 @@ class Vote < ActiveRecord::Base ...@@ -2,4 +2,7 @@ class Vote < ActiveRecord::Base
belongs_to :user belongs_to :user
belongs_to :candidate belongs_to :candidate
validates :candidate_id, presence: true validates :candidate_id, presence: true
def to_s
"#{candidate.first_name} #{candidate.last_name}"
end
end end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<%= f.input :first_name %> <%= f.input :first_name %>
<%= f.input :last_name %> <%= f.input :last_name %>
<%= f.input :nickname %> <%= f.input :nickname %>
<%= f.association :position %> <%= f.association :position, include_blank: false %>
<%= f.input :slogan %> <%= f.input :slogan %>
<%= f.submit %> <%= f.submit %>
<% end %> <% end %>
\ No newline at end of file
...@@ -4,12 +4,15 @@ ...@@ -4,12 +4,15 @@
<hr> <hr>
<% @positions.each do |p| %> <% @positions.each do |p| %>
<h2><%= p.name %></h2> <h2><%= p.name %></h2>
<ul><% p.candidates.each do |candidate| %></ul> <ul><% p.candidates.each do |candidate| %>
<li>
<%= candidate.first_name %> <%= candidate.last_name %> <%= candidate.first_name %> <%= candidate.last_name %>
<%= link_to "Show", admin_candidate_path(candidate.id) %> <%= link_to "Show", admin_candidate_path(candidate.id) %>
<%= link_to "Edit", edit_admin_candidate_path(candidate.id) %> <%= link_to "Edit", edit_admin_candidate_path(candidate.id) %>
<%= link_to "Delete", admin_candidate_path(candidate.id), method: :delete, data: { confirm: 'Are you sure?' } %> <%= link_to "Delete", admin_candidate_path(candidate.id), method: :delete, data: { confirm: 'Are you sure?' } %>
</li>
<% end %> <% end %>
</ul>
<% end %> <% end %>
<h2>Candidate: <%= @candidate.first_name %> <%= @candidate.last_name %> </h2> <h2>Candidate: <%= @candidate.first_name %> <%= @candidate.last_name %> </h2>
<h3><%= @candidate.slogan %></h3> <h3><%= @candidate.slogan %></h3>
<ul> <ul>
<li>Votes: <% @votes_male.size + @votes_female.size %></li> <li>Votes: <%= @votes_male.size + @votes_female.size %></li>
<li>Male Voters: <% @votes_male.size %></li> <li>Male Voters: <%= @votes_male.size %></li>
<li>Female Voters: <% @votes_female.size %></li> <li>Female Voters: <%= @votes_female.size %></li>
</ul> </ul>
<%= link_to "Back to Candidates", admin_candidates_path %> <%= link_to "Back to Candidates", admin_candidates_path %>
\ No newline at end of file
<h1>Votes</h1> <h1>Votes</h1>
<%= link_to "Vote Now!", new_vote_path %>
<table width="100%"> <table width="100%">
<tr> <tr>
<th>#</th> <th>#</th>
...@@ -7,12 +7,13 @@ ...@@ -7,12 +7,13 @@
<th>Candidate</th> <th>Candidate</th>
<th>Position</th> <th>Position</th>
</tr> </tr>
<% @votes.each do |vote| %> <% @votes.each do |vote| %>
<tr> <tr>
<td><%= link_to "#{vote.id}", vote_path(vote.id) %></td> <td><%= link_to "#{vote.id}", vote_path(vote.id) %></td>
<td><%= vote.user_id %></td> <td><%= vote.user_id %></td>
<td> <td>
<%= vote.candidate.first_name %> " <%= vote.candidate.nickname %> " <%= vote.candidate.last_name %> <%= vote.candidate.first_name %> "<%= vote.candidate.nickname %>" <%= vote.candidate.last_name %>
</td> </td>
<td> <td>
<%= vote.candidate.position.name %> <%= vote.candidate.position.name %>
......
<h1>Vote</h1>
<%= simple_form_for(@vote) do |v| %>
<% @positions.each do |p| %>
<h2><%= p.name %></h2>
<%= v.association :candidate, :collection => Candidate.where(position_id: (p.id)), include_blank: false %>
<%= v.input :comments %><br>
<%= v.button :submit, "Vote" %>
<% end %>
<% end %>
<h1>My Vote Record</h1>
<table class="table">
<thead>
<th>Candidate</th>
<th>Position</th>
<th>Comments</th>
</thead>
<tbody>
<% if !@votes.empty? %>
<% @positions.each do |positions| %>
<% current_user.votes.each do |blah| %>
<% @candidates.each do |candidates| %>
<% if candidates.position.name == positions.name %>
<tr>
<td><%= candidates.first_name %> <%= candidates.last_name %></td>
<td><%= candidates.position.name %></td>
<td><%= blah.comments %></td>
</tr>
<% end %>
<% end %>
<% end %>
<% end %>
<% else %>
<tr>
<td colspan="3"><em>No Votes Found</em></td>
</tr>
<% end %>
</tbody>
</table>
<a href="http://localhost:3000/votes/new" class="btn btn-primary" role="button">Add Vote</a>
\ 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