Commit 168e4c7b authored by Norence Ilicito's avatar Norence Ilicito

voting module, incomplete

parent 94a5cd72
...@@ -2,4 +2,16 @@ class ApplicationController < ActionController::Base ...@@ -2,4 +2,16 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead. # For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:email, :password,
:password_confirmation, :first_name, :last_name, :gender, :birthday])
# devise_parameter_sanitizer.permit(:sign_in, keys: [:email, :password])
devise_parameter_sanitizer.permit(:account_update, keys: [:email, :password,
:password_confirmation, :current_password, :first_name, :last_name, :gender, :birthday])
end
end end
class VotesController < ApplicationController
before_action :authenticate_user!, except: [:index]
def index
@candidates = Candidate.all.group_by(&:position_id)
end
def vote
@vote = Vote.new
@candidates = Candidate.all.group_by(&:position_id)
end
def confirm_vote
@vote = Vote.new(vote_params)
@vote.user = current_user
if @vote.save(vote_params)
if current_user.votes.count >= Position.count
redirect_to root_path, notice: "You have already exceeded your number of votes."
else
redirect_to :vote, notice: "Vote successful!"
end
else
render :vote
end
end
def vote_history
@votes = current_user.votes
end
private
def vote_params
params.require(:vote).permit!
end
end
class Vote < ActiveRecord::Base
end
<link rel"stylesheet" type="text/css" href="application.css">
<%= simple_form_for(@candidate) do |c| %> <%= simple_form_for(@candidate) do |c| %>
<%= c.input :id %> <%= c.input :id %>
<%= c.input :first_name %> <%= c.input :first_name %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h1>Edit Candidate</h1> <h1>Edit Candidate</h1>
<%= render partial: "form" %> <%= render partial: "form" %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h1>New Candidate</h1> <h1>New Candidate</h1>
<%= render partial: "form" %> <%= render partial: "form" %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h1> <h1>
<%= @candidate.first_name %> <%= @candidate.first_name %>
<%= @candidate.last_name %> <%= @candidate.last_name %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h2>Resend confirmation instructions</h2> <h2>Resend confirmation instructions</h2>
<%= simple_form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %> <%= simple_form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<p>Welcome <%= @email %>!</p> <p>Welcome <%= @email %>!</p>
<p>You can confirm your account email through the link below:</p> <p>You can confirm your account email through the link below:</p>
......
<link rel"stylesheet" type="text/css" href="application.css">
<p>Hello <%= @resource.email %>!</p> <p>Hello <%= @resource.email %>!</p>
<p>We're contacting you to notify you that your password has been changed.</p> <p>We're contacting you to notify you that your password has been changed.</p>
<link rel"stylesheet" type="text/css" href="application.css">
<p>Hello <%= @resource.email %>!</p> <p>Hello <%= @resource.email %>!</p>
<p>Someone has requested a link to change your password. You can do this through the link below.</p> <p>Someone has requested a link to change your password. You can do this through the link below.</p>
......
<link rel"stylesheet" type="text/css" href="application.css">
<p>Hello <%= @resource.email %>!</p> <p>Hello <%= @resource.email %>!</p>
<p>Your account has been locked due to an excessive number of unsuccessful sign in attempts.</p> <p>Your account has been locked due to an excessive number of unsuccessful sign in attempts.</p>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h2>Change your password</h2> <h2>Change your password</h2>
<%= simple_form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %> <%= simple_form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h2>Forgot your password?</h2> <h2>Forgot your password?</h2>
<%= simple_form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %> <%= simple_form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h2>Edit <%= resource_name.to_s.humanize %></h2> <h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h2>Sign up</h2> <h2>Sign up</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
...@@ -7,7 +10,7 @@ ...@@ -7,7 +10,7 @@
<%= f.input :first_name, required: true, autofocus: true %> <%= f.input :first_name, required: true, autofocus: true %>
<%= f.input :last_name, required: true, autofocus: true %> <%= f.input :last_name, required: true, autofocus: true %>
<%= f.input :birthday, required: true, autofocus: true %> <%= f.input :birthday, required: true, autofocus: true %>
<%= f.input :gender, required: true, autofocus: true %> <%= f.input :gender, required: true, as: :select, :collection => ['Female', 'Male'] autofocus: true %>
<%= f.input :email, required: true, autofocus: true %> <%= f.input :email, required: true, autofocus: true %>
<%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %> <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
<%= f.input :password_confirmation, required: true %> <%= f.input :password_confirmation, required: true %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h2>Log in</h2> <h2>Log in</h2>
<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> <%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<%- if controller_name != 'sessions' %> <%- if controller_name != 'sessions' %>
<%= link_to "Log in", new_session_path(resource_name) %><br /> <%= link_to "Log in", new_session_path(resource_name) %><br />
<% end -%> <% end -%>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h2>Resend unlock instructions</h2> <h2>Resend unlock instructions</h2>
<%= simple_form_for(resource, as: resource_name, url: unlock_path(resource_name), html: { method: :post }) do |f| %> <%= simple_form_for(resource, as: resource_name, url: unlock_path(resource_name), html: { method: :post }) do |f| %>
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
<div class="box fade-in one"> <div class="box fade-in one">
<h1 style="margin-left:100px;margin-top:100px;font-family:Droid Sans">Election 2016</h1> <h1 style="margin-left:100px;margin-top:100px;font-family:Droid Sans">Election 2016</h1>
<h3 style="color:#0080ff;margin-left:100px"> <h3 style="color:#0080ff;margin-left:100px">
...@@ -32,6 +33,7 @@ ...@@ -32,6 +33,7 @@
<th>First Name</th> <th>First Name</th>
<th>Last Name</th> <th>Last Name</th>
<th>Slogan</th> <th>Slogan</th>
<th>Number of Votes</th>
</tr> </tr>
<% @candidates.each do |candidate| %> <% @candidates.each do |candidate| %>
<tr> <tr>
...@@ -39,6 +41,7 @@ ...@@ -39,6 +41,7 @@
<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.votes.count %> </td>
</tr> </tr>
<% end %> <% end %>
</table> </table>
......
<link rel"stylesheet" type="text/css" href="application.css"> <link rel"stylesheet" type="text/css" href="application.css">
<h1>Election 2016</h1> <h1>Election 2016</h1>
<h3>Hello <%= current_user.last_name %></h3> <h3>Hello <%= current_user.first_name%> <%= current_user.last_name %>!</h3>
<ul>
<li> Email: <%= current_user.email %> </li>
<li> Birthday: <%= current_user.birthday %> </li>
<li> Sex: <%= current_user.gender %> </li>
<li> Password: <%= current_user.password %> </li>
</ul>
<table width="100%" style="margin:100px;"> <table width="100%" style="margin:100px;">
...@@ -17,13 +24,13 @@ ...@@ -17,13 +24,13 @@
<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> <!--<td>
<%= link_to "Show ", candidate_path(candidate.id) %> | <%= link_to "Show ", candidate_path(candidate.id) %> |
<%= link_to "Edit", edit_candidate_path(candidate.id) %> | <%= link_to "Edit", edit_candidate_path(candidate.id) %>
<%= link_to "Delete", candidate_path(candidate.id), method: :delete, data: { confirm: 'Are you sure?'} %> <%= link_to "Delete", candidate_path(candidate.id), method: :delete, data: { confirm: 'Are you sure?'} %>
</td> </td> -->
</tr> </tr>
<% end %> <% end %>
</table> </table>
\ No newline at end of file
<h1>Voting Statistic</h1>
\ No newline at end of file
<link rel"stylesheet" type="text/css" href="application.css">
<%= simple_form_for(@position) do |p| %> <%= simple_form_for(@position) do |p| %>
<%= p.input :name, as: :select, collection: ["President", "Vice President", "Senator"] %> <%= p.input :name, as: :select, collection: ["President", "Vice President", "Senator"] %>
<%= p.submit %> <%= p.submit %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h1>Edit Candidate</h1> <h1>Edit Candidate</h1>
<%= render partial: "form" %> <%= render partial: "form" %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h1>Add Position</h1> <h1>Add Position</h1>
<%= render partial: "form" %> <%= render partial: "form" %>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h1> <h1>
<%= @position.name %> <%= @position.name %>
</h1> </h1>
......
<link rel"stylesheet" type="text/css" href="application.css">
<h1>Ballot</h1>
<% @candidates.each do |position_id, candidates| %>
<%= simple_form_for(@vote, url: confirm_vote_path, method: :post) do |o| %>
<%= Position.find(position_id).name %>
<ul>
<li><%= o.association :candidate, :collection => candidates %></li>
<li><%= o.input :comments %></li>
</ul>
<%= o.button :submit, "Vote" %>
<% end %>
<%= link_to "Back", candidates_list_path %>
<% end %>
\ No newline at end of file
<link rel"stylesheet" type="text/css" href="application.css">
<h1>Voting Record of <%= current_user.first_name %> <%= current_user.last_name %> </h1>
<table>
<thead>
<th>Voted Candidate</th>
<th>Position </th>
<th>Voted On</th>
</thead>
<tbody>
<% @votes.each do |o| %>
<tr>
<td><%= o.candidate.first_name %>&nbsp<%= o.candidate.last_name %></td>
<td><%= o.candidate.position.name %></td>
<td><%= o.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
class CreateVotes < ActiveRecord::Migration
def change
create_table :votes do |t|
t.integer :user_id
t.integer :candidate_id
t.text :comments
t.timestamps null: false
end
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160718054341) do ActiveRecord::Schema.define(version: 20160719105648) do
create_table "candidates", force: :cascade do |t| create_table "candidates", force: :cascade do |t|
t.string "first_name" t.string "first_name"
...@@ -50,4 +50,12 @@ ActiveRecord::Schema.define(version: 20160718054341) do ...@@ -50,4 +50,12 @@ ActiveRecord::Schema.define(version: 20160718054341) do
add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
create_table "votes", force: :cascade do |t|
t.integer "user_id"
t.integer "candidate_id"
t.text "comments"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end end
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
user_id: 1
candidate_id: 1
comments: MyText
two:
user_id: 1
candidate_id: 1
comments: MyText
require 'test_helper'
class VoteTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# 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