Commit 0c21c196 authored by Bianca Tarun's avatar Bianca Tarun

fixed votes form and controller

parent 4d236ef5
......@@ -49,3 +49,4 @@ gem 'simple_form'
gem 'devise'
gem 'better_errors'
\ No newline at end of file
......@@ -38,10 +38,15 @@ GEM
tzinfo (~> 1.1)
arel (6.0.3)
bcrypt (3.1.11-x86-mingw32)
better_errors (2.1.1)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
rack (>= 0.9.0)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (9.0.5)
coderay (1.1.1)
coffee-rails (4.1.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.1.x)
......@@ -162,6 +167,7 @@ PLATFORMS
x86-mingw32
DEPENDENCIES
better_errors
byebug
coffee-rails (~> 4.1.0)
devise
......
class ElectionController < ApplicationController
def vote
if Vote.exists?(candidate_id: params[:id], user_id: current_user.id)
render "candidates/show.html.erb"
else
redirect_to new_vote_path
end
end
end
\ No newline at end of file
......@@ -3,6 +3,7 @@ class PartyListsController < ApplicationController
def index
@party_lists = PartyList.all
@candidates = Candidate.all
@positions = Position.all
render "party_lists/index.html.erb"
end
end
\ No newline at end of file
......@@ -2,37 +2,26 @@ class VotesController < ApplicationController
before_action :authenticate_user!
def index
@candidates_by_position = Candidate.all.group_by(&:position_id)
@votes = Vote.all
end
def new
if Vote.exists?(user: current_user)
redirect_to root_path, alert: "You have already voted."
else
@user = current_user.votes.build
@vote = Vote.new
@user = current_user
@user.votes.build
@positions = Position.all
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 =>'new'
end
end
def checkout
if current_user.update(vote_params)
if current_user.update(vote_params())
redirect_to root_path
else
@user = current_user
@positions = Position.all
redirect_to vote_path, alert: "Please accomplish all required fields."
redirect_to votes_new_path, alert: "Please accomplish all required fields."
end
end
......
......@@ -2,6 +2,8 @@ class Candidate < ActiveRecord::Base
validates :first_name, presence: true
validates :last_name, presence: true
validates :slogan, uniqueness: true, presence: true
validates :party_list, presence: true
validates :position, presence: true
belongs_to :position
belongs_to :party_list
has_many :votes
......
class PartyList < ActiveRecord::Base
has_many :candidates
validates :name, presence: true, uniqueness: true
validates :description, presence: true
end
......@@ -4,4 +4,5 @@ class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :votes
accepts_nested_attributes_for :votes
end
......@@ -2,8 +2,8 @@
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :nickname %>
<%= f.association :party_list, include_blank: false %>
<%= f.association :position, include_blank: false %>
<%= f.association :party_list, as: :select, include_blank: true, required: true %>
<%= f.association :position, as: :select, include_blank: true, required: true %>
<%= f.input :slogan %>
<%= f.submit %>
<% end %>
\ No newline at end of file
<h1>Admin Candidates</h1>
<hr>
<%= link_to("New Candidate", new_admin_candidate_path) %>
<button type="button" class="btn btn-default"><%= link_to("New Candidate", new_admin_candidate_path) %></button>
<hr>
<% @positions.each do |p| %>
<h2><%= p.name %></h2>
<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>
<h5>
Name: <%= candidate.first_name %> <%= candidate.last_name %> | Actions: <%= 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?' } %>
</h5>
<% end %>
</ul>
<% end %>
......
......@@ -7,17 +7,3 @@
<li>Female Voters: <%= @votes_female.size %></li>
</ul>
<<<<<<< HEAD
<%= link_to "Votes for Candidate", "/election/vote/#{@candidate.id}" %>
=======
<h2>People who voted for <%= @candidate.first_name %> <%= @candidate.last_name %>:</h2>
<ul>
<li>User - Comment</li>
<% @votes.each do |v| %>
<li>
<%= v.user.email %> - <%= v.comment %>
</li>
<% end %>
</ul>
>>>>>>> 4a293966901c2565a4eea2574a3086d230d926fc
<%= link_to "Back to Candidates", admin_candidates_path %>
\ No newline at end of file
<h1>Admin Party Lists</h1>
<hr>
<%= link_to("New Party List", new_admin_party_list_path) %>
<button type="button" class="btn btn-default"><%= link_to("New Party List", new_admin_party_list_path) %></button>
<hr>
<table width="100%", class="table table-hover">
<tr>
<th>Name</th>
</tr>
<% @party_lists.each do |p| %>
<tr>
<td><%= p.name %></td>
<td>
<%= link_to "Show", admin_party_list_path(p.id) %>
<%= link_to "Edit", edit_admin_party_list_path(p.id) %>
<%= link_to "Delete", admin_party_list_path(p.id), method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
</tr>
</table>
<h3><%= p.name %><h3>
<ul><h4>Candidates</h4></ul>
<ul><% p.candidates.each do |candidate| %>
<li>
<%= candidate.first_name %> <%= candidate.last_name %>
</li>
<h5><%= candidate.first_name %> <%= candidate.last_name %></h5>
<% end %>
</ul>
<h5>Actions: <%= link_to "Show", admin_party_list_path(p.id) %> <%= link_to "Edit", edit_admin_party_list_path(p.id) %> <%= link_to "Delete", admin_party_list_path(p.id), method: :delete, data: { confirm: 'Are you sure?' } %>
</h5>
<hr>
<% end %>
<hr>
......@@ -3,7 +3,7 @@
<h3>Party List Description </h3>
<%= @party_list.description %>
<% @party_list.candidates.each do |candidate| %>
<ul><%= candidate.first_name %> <%= candidate.last_name %>
<ul>Name: <%= candidate.first_name %> <%= candidate.last_name %> </ul>
<% end %>
<hr>
<%= link_to "Back to Party Lists", admin_party_lists_path %>
\ No newline at end of file
<h1>Admin Positions</h1>
<hr>
<%= link_to("New Position", new_admin_position_path) %>
<button type="button" class="btn btn-default"><%= link_to("New Position", new_admin_position_path) %></button>
<hr>
<table width="100%", class="table table-hover">
<tr>
......
......@@ -4,6 +4,7 @@
<table class="table table-hover", width="100%">
<tr>
<th>Full Name</th>
<th>Actions</th>
</tr>
<% p.candidates.each do |candidate| %>
<tr>
......
......@@ -12,7 +12,7 @@
<li>User - Comment</li>
<% @votes.each do |v| %>
<li>
<%= v.user.email %> - <%= v.comment %>
<%= v.user.email %> - <%= v.comments %>
</li>
<% end %>
</ul>
......
......@@ -7,10 +7,11 @@
<button type="button" class="btn btn-default"><%= link_to "Manage Party Lists", admin_party_lists_path %></button>
<button type="button" class="btn btn-default"><%= link_to "Vote", votes_new_path %></button>
<button type="button" class="btn btn-default"><%= link_to "About", pages_about_path %></button>
<%= link_to "User Profile", votes_user_profile_path %></button>
<button type="button" class="btn btn-default"><%= link_to "User Profile", votes_user_profile_path %></button>
<% else %>
<button type="button" class="btn btn-default"><%= link_to "About", pages_about_path %></button>
<button type="button" class="btn btn-default"><%= link_to "Vote", votes_new_path %></button>
<button type="button" class="btn btn-default"><%= link_to "Vote Record", votes_index_path %></button>
<button type="button" class="btn btn-default"><%= link_to "User Profile", votes_user_profile_path %></button>
<% end %>
<hr>
......
<h1>Party Lists</h1>
<% @party_lists.each do |p| %>
<%= p.name %>
<hr>
<h3>Party List Description</h3>
<h3><%= p.name %></h3>
<h4>Party List Description</h4>
<%= p.description %>
<h3>Party List Candidates</h3>
<h4>Party List Candidates</h4>
<% p.candidates.each do |candidate| %>
<%= candidate.first_name %> <%= candidate.last_name %>
<% end %>
<hr>
<% end %>
<hr>
<%= link_to "Back to Homepage", root_path %>
\ No newline at end of file
<h1>Vote for your candidates.</h1>
<%= simple_form_for @vote do |v| %>
<%= simple_form_for(@user, url: checkout_path, method: :post) do |v| %>
<% @positions.each do |p| %>
<%= v.simple_fields_for :votes do |f| %>
<h2><%= p.name %></h2>
<div class="row">
<div class="col-md-6">
<%= f.association :candidate, :collection => Candidate.where(position_id: (p.id)), include_blank: false, :label_method => lambda {|candidate| "#{candidate.last_name}, #{candidate.first_name}"}, as: :radio_buttons , label: "Candidates: "%>
<% Candidate.where(position_id: (p.id)).each do |candidate| %>
<p><div class="btn btn-primary btn-xs" data-toggle="popover" data-content="<%= candidate.slogan %>" style="margin-top: 2px; margin-bottom: 1px", data-trigger="focus", role="button", tabindex="0", title="<%= candidate.first_name %> <%= candidate.last_name %>"> View <%= candidate.last_name %>'s Slogan</div></p>
<% end -%>
</div>
<div class="col-md-6">
<%= v.association :candidate, include_blank: false, collection: @candidates %>
<br><br><br>
</div>
</div>
<%= f.input :comments %>
<% end -%>
<% end -%>
<%= v.input :comments %><br>
<%= v.button :submit, "Submit" %>
<%= v.button :submit, "Vote", class: "btn btn-primary" %>
<%= link_to "Back to Homepage", root_path, class: "btn btn-default" %>
<%= link_to "Vote Record", votes_index_path, class: "btn btn-default" %>
<% end %>
<script>
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
</script>
\ No newline at end of file
<h1>Votes</h1>
<%= link_to "Vote Now!", new_vote_path %>
<%= link_to "Vote Now!", votes_new_path %>
<table width="100%">
<tr>
<th>#</th>
<th>User ID</th>
<th>Candidate</th>
<th>Position</th>
<th>Comments</th>
</tr>
<% @votes.each do |vote| %>
......@@ -18,6 +19,7 @@
<td>
<%= vote.candidate.position.name %>
</td>
<td><%= vote.comments %></td>
</tr>
<% end %>
</table>
\ No newline at end of file
<h1>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>
<hr>
<%= link_to "Vote", votes_path %>
<%= link_to "Back to Homepage", root_path %>
\ No newline at end of file
<h1>Profile Page</h1>
<h2><%= current_user.id %> <%= current_user.first_name %> <%= current_user.last_name %></h2>
<h2><%= current_user.first_name %> <%= current_user.last_name %></h2>
<hr>
<h2>Positions</h2>
<h2>Candidates Voted For</h2>
<% @positions.each do |p| %>
<h3><%= p.name %></h3>
<% p.candidates.each do |c| %>
<% c.votes.each do |v| %>
<% if v.user_id == current_user.id %>
<%= v.candidate.first_name %> <%= v.candidate.last_name %> <%= v.comments %>
Name: <%= v.candidate.first_name %> <%= v.candidate.last_name %> || Comments: <%= v.comments %>
<% else %>
<% end %>
<% end %>
......
......@@ -16,7 +16,6 @@ Rails.application.routes.draw do
get'/party_lists/index', to: "party_lists#index"
get'/votes/user_profile', to: "votes#profile"
get'/votes/index', to: "votes#index"
get'/election/vote/:id', to: "election#vote"
post'/checkout', to: 'votes#checkout', as: :checkout
end
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