added views, modified controller for votes

parent efd7db55
class VotesController < ApplicationController
def index
@votes = Vote.all
@votes = current_user.votes
@candidates = Candidate.all
@positions = Position.all
render "votes/index.html.erb"
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
\ No newline at end of file
......@@ -4,4 +4,7 @@ class Candidate < ActiveRecord::Base
validates :slogan, uniqueness: true, presence: true
belongs_to :position
has_many :votes
def to_s
"#{first_name} #{last_name}"
end
end
......@@ -2,4 +2,7 @@ class Vote < ActiveRecord::Base
belongs_to :user
belongs_to :candidate
validates :candidate_id, presence: true
def to_s
"#{candidate.first_name} #{candidate.last_name}"
end
end
......@@ -2,7 +2,7 @@
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :nickname %>
<%= f.association :position %>
<%= f.association :position, include_blank: false %>
<%= f.input :slogan %>
<%= f.submit %>
<% end %>
\ No newline at end of file
......@@ -4,12 +4,15 @@
<hr>
<% @positions.each do |p| %>
<h2><%= p.name %></h2>
<ul><% p.candidates.each do |candidate| %></ul>
<ul><% p.candidates.each do |candidate| %>
<li>
<%= candidate.first_name %> <%= candidate.last_name %>
<%= link_to "Show", 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?' } %>
</li>
<% end %>
</ul>
<% end %>
<h2>Candidate: <%= @candidate.first_name %> <%= @candidate.last_name %> </h2>
<h3><%= @candidate.slogan %></h3>
<ul>
<li>Votes: <% @votes_male.size + @votes_female.size %></li>
<li>Male Voters: <% @votes_male.size %></li>
<li>Female Voters: <% @votes_female.size %></li>
<li>Votes: <%= @votes_male.size + @votes_female.size %></li>
<li>Male Voters: <%= @votes_male.size %></li>
<li>Female Voters: <%= @votes_female.size %></li>
</ul>
<%= link_to "Back to Candidates", admin_candidates_path %>
\ No newline at end of file
<h1>Votes</h1>
<%= link_to "Vote Now!", new_vote_path %>
<table width="100%">
<tr>
<th>#</th>
......@@ -7,12 +7,13 @@
<th>Candidate</th>
<th>Position</th>
</tr>
<% @votes.each do |vote| %>
<tr>
<td><%= link_to "#{vote.id}", vote_path(vote.id) %></td>
<td><%= vote.user_id %></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>
<%= 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