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

fixed votes form and controller

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