Commit e3fdc7e2 authored by Christiana Tan's avatar Christiana Tan

Updated file

parent 6c386672
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
// Place all the styles related to the Votes controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class CandidatesController < ApplicationController
before_action :authenticate_user!, except: [:index] #or except
before_action :authenticate_admin!, except: [:index]
def index
@candidates = Candidate.all
end
def vote
end
def new
@candidate = Candidate.new
end
......@@ -16,7 +13,7 @@ class CandidatesController < ApplicationController
@candidate = Candidate.new(candidate_params)
if @candidate.save
redirect_to candidate_path(@candidate), notice: "User was successfully created"
redirect_to candidates_path, notice: "User was successfully created"
else
render 'new'
end
......@@ -26,7 +23,7 @@ class CandidatesController < ApplicationController
@candidate = Candidate.find(params[:id])
@candidate.destroy()
redirect_to candidate_path
redirect_to candidates_path
end
......@@ -35,6 +32,13 @@ class CandidatesController < ApplicationController
end
def update
@candidate = Candidate.find(params[:id])
if @candidate.update(candidate_params)
redirect_to candidates_path
else
render 'edit'
end
end
def show
......
class VotesController < ApplicationController
before_action :authenticate_user!, except: [:index]
def index
@candidates = Candidate.all
end
def voting
@vote = Vote.new
end
end
module VotesHelper
end
......@@ -4,4 +4,6 @@ class User < ActiveRecord::Base
has_many :votes
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
class Vote < ActiveRecord::Base
belongs_to :user_id
belongs_to :candidate_id
belongs_to :user
belongs_to :candidate
validates :candidate, presence: true, uniqueness: true
end
......@@ -4,7 +4,7 @@
<%= c.input :nickname %>
<%= c.input :slogan, as: :text %>
<%= c.association :position %>
<%= c.submit %>
<%= c.button :submit, "Submit", class: "btn btn-success" %>
<%= link_to 'Cancel', candidates_path, class: "btn btn-danger" %>
<% end %>
<%= link_to 'Cancel', candidates_path, class: "btn btn-danger" %>
\ No newline at end of file
<div class = "container">
<h1>Edit Candidate</h1>
<%= render 'form' %>
</div>
\ No newline at end of file
<div class = "container">
<%#- <%= current_admin.email %>
<% @candidates.group_by(&:position).each do |p, can| %>
<% @candidates.group_by(&:position).each do |p, can| %>
<h4> <%= p.name %> </h4>
<table class = "table">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Nickname</th>
<% if !admin_signed_in? %>
<th>Votes</th>
<% end %>
<% if admin_signed_in? %>
<th>Slogan</th>
<% end %>
</thead>
<tbody>
<% can.each do |c| %>
<tr>
<td> <%= c.first_name %> </td>
<td> <%= c.last_name %> </td>
<td> <%= c.nickname %></td>
<% if !admin_signed_in? %>
<td> <%= %></td>
<% end %>
<% if admin_signed_in? %>
<td> <%= c.votes.count%></td>
<td> <%= c.slogan %>
<td> <%= link_to 'Show', show_candidates_path(c), class: "btn btn-primary" %></td>
<td> <%= link_to 'Edit', edit_candidates_path(c), class: "btn btn-success" %></td>
<td> <a href="/candidates/delete?id=<%= c.id %>"><button class="btn btn-danger">Delete</button></a></td>
<% end %>
<td> <%= link_to 'Show', candidate_path(c), class: "btn btn-primary" %></td>
<td> <%= link_to 'Edit', edit_candidate_path(c), class: "btn btn-success" %></td>
<td> <%= link_to 'Delete', candidate_path(c), method: :delete, data: { confirm: "Are you sure you want to delete?" }, class: "btn btn-danger" %> </td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% if admin_signed_in? %>
<%= link_to 'New Candidate', new_candidates_path, class: "btn btn-primary" %>
<% end %>
<% if user_signed_in? %>
<%= link_to 'Vote', vote_candidates_path, class: "btn btn-primary" %>
<% end %>
<% end %>
<%= link_to 'New Candidate', new_candidate_path, class: "btn btn-primary" %>
</div>
\ No newline at end of file
<h1>New Candidate</h1>
<!--<%= form_for(@candidate) do |u| %>
<% if @candidate.errors.any? %>
<ul>
<%= @candidate.errors.full_messages.each do |message| %>
<li> <%= message %>
</li>
<% end %>
</ul>
<% end %>
<p>
<%= u.label "First Name: " %>
<%= u.text_field :first_name %>
</p>
<p>
<%= u.label "Last Name: " %>
<%= u.text_field :last_name %>
</p>
<p>
<%= u.label "Nickname: " %>
<%= u.text_field :nickname %>
</p>
<p>
<%= u.label "Slogan" %>
<%= u.text_area :slogan %>
<% if @candidate.errors.messages[:slogan].any? %>
<% end %>
</p>
<button class = "btn btn-success"> Create Candidate<% u.submit %></button>
<br>
<br>
<% end %>
<%= link_to 'Cancel', candidates_path, class: "btn btn-danger" %> -->
<%= render 'form' %>
\ No newline at end of file
<div class = "container">
<h1>New Candidate</h1>
<%= render 'form' %>
</div>
\ No newline at end of file
<h1> <%= @candidate.first_name %> <%= @candidate.last_name %></h1>
<table>
<div class = "container">
<h1> <%= @candidate.first_name %> <%= @candidate.last_name %></h1>
<table class = "table">
<thead>
<th>Nickname</th>
<th>Slogan</th>
<th>Votes</th>
</thead>
<tbody>
<tr>
<td> <%= @candidate.nickname %></td>
<td> <%= @candidate.slogan %></td>
</tr>
</tbody>
</table>
<%= link_to 'Back', candidates_path, class: "btn btn-primary" %>
\ No newline at end of file
</table>
<%= link_to 'Back', candidates_path, class: "btn btn-primary" %>
</div>
\ No newline at end of file
......@@ -15,7 +15,8 @@
</div>
<ul class="nav navbar-nav">
<li><a href="positions">View Positions</a></li>
<li><%= link_to 'View Positions', positions_path %></a></li>
<li><%= link_to 'View Candidates', candidates_path %></a></li>
</ul>
......
<%= simple_form_for(@position) do |p| %>
<%= p.input :name %>
<%= p.input :number_of_winners %>
<%= p.button :submit, "Submit", class: "btn btn-success" %>
<%= link_to 'Cancel', candidates_path, class: "btn btn-danger" %>
<% end %>
<h1>Edit Position</h1>
<%= form_for(@position, url: update_position_path(@position)) do |p| %>
<p>
<%= p.label "Name" %>
<%= p.text_field :name %>
</p>
<div class = "container">
<h1>Edit Position</h1>s
<p>
<%= p.label "Number of Winners" %>
<%= p.text_field :number_of_winners %>
</p>
<button class = "btn btn-success">Edit position <% p.submit %></button>
<% end %>
<%= link_to 'Cancel', positions_path, class: "btn btn-danger" %>
<%= render 'form' %>
</div>
\ No newline at end of file
<h1> List of Positions</h1>
<table class = "table">
<div class = "container">
<h1> List of Positions</h1>
<table class = "table">
<thead>
<th>Name</th>
<th>Number of Winners</th>
</thead>
<tbody>
<% @positions.each do |c| %>
<tr>
<td> <%= c.name %> </td>
<td> <%= link_to 'Show', show_position_path(c) %> </td>
<td> <%= link_to 'Edit', edit_position_path(c) %> </td>
<td> <%= link_to 'Delete', delete_position_path(c) %> </td>
<td> <%= c.number_of_winners %></td>
<td> <%= link_to 'Show', position_path(c), class: "btn btn-primary" %> </td>
<td> <%= link_to 'Edit', edit_position_path(c), class: "btn btn-success" %> </td>
<td> <%= link_to 'Delete', position_path(c), method: :delete, data: { confirm: "Are you sure you want to delete?" }, class: "btn btn-danger" %> </td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'Add new position', new_positions_path, class: "btn btn-primary" %>
\ No newline at end of file
</table>
<%= link_to 'Add new position', new_position_path, class: "btn btn-primary" %>
</div>
\ No newline at end of file
<h1>New Position</h1>
<%= form_for(@position) do |p| %>
<p>
<%= p.label "Name" %>
<%= p.text_field :name %>
</p>
<p>
<%= p.label "Number of Winners" %>
<%= p.text_field :number_of_winners %>
</p>
<button class = "btn btn-success">Create new position <% p.submit %></button>
<% end %>
<%= link_to 'Cancel', positions_path, class: "btn btn-danger" %>
<div class = "container">
<h1>New Position</h1>
<%= render 'form' %>
</div>
\ No newline at end of file
<h1>Candidates for <%= @position.name %></h1>
<div class = "container">
<h1>Candidates for <%= @position.name %></h1>
<table class = "table">
<table class = "table">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Nickname</th>
<th>Slogan</th>
<th>Votes</th>
</thead>
<tbody>
<% @position.candidate.each do |c| %>
<% @position.candidates.each do |c| %>
<tr>
<td> <%= c.first_name %> </td>
<td> <%= c.last_name %> </td>
<td> <%= c.nickname %></td>
<td> <%= c.slogan %></td>
<td> <%= c.votes.count %></td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'Back', positions_path %>
\ No newline at end of file
</table>
<%= link_to 'Back', positions_path, class: "btn btn-primary" %>
</div>
\ No newline at end of file
<div class = "container">
<% @candidates.group_by(&:position).each do |p, can| %>
<h4> <%= p.name %> </h4>
<table class = "table">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Nickname</th>
<th>Votes</th>
</thead>
<tbody>
<% can.each do |c| %>
<tr>
<td> <%= c.first_name %> </td>
<td> <%= c.last_name %> </td>
<td> <%= c.nickname %></td>
<td> <%= c.votes.count%></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<%= link_to 'Vote', voting_path, class: "btn btn-primary" %>
</div>
\ No newline at end of file
<div class = "container">
<h1>Vote for a Candidate</h1>
<%= simple_form_for(@) do |c| %>
</div>
Rails.application.routes.draw do
resources :candidates
resources :positions
devise_for :users
devise_for :admins
root 'candidates#index'
get '/candidates', to: 'candidates#index', as: :candidates
get '/candidates/new', to: 'candidates#new', as: :new_candidates
get '/candidates/vote', to: 'candidates#vote', as: :vote_candidates
get 'candidates/edit/:id', to: 'candidates#edit', as: :edit_candidates
get '/candidates/show/:id', to: 'candidates#show', as: :show_candidates
patch 'candidates/update/:id', to: 'candidates#update', as: :update_candidates
get '/candidates/delete', to: 'candidates#destroy', as: :delete_candidates
get '/votes/index', to: 'votes#index', as: :votes
post '/candidates', to: 'candidates#create', as: :candidate
get 'voting', to: 'votes#voting', as: :voting
root 'votes#index'
get 'positions', to:'positions#index', as: :positions
get 'positions/new', to:'positions#new', as: :new_positions
patch 'positions/update/:id', to: 'positions#update', as: :update_position
post 'positions', to: 'positions#create', as: :create_position
get 'positions/edit/:id', to: 'positions#edit', as: :edit_position
get 'positions/show/:id', to: 'positions#show', as: :show_position
get '/positions/delete/:id', to: 'positions#destroy', as: :delete_position
devise_for :users
devise_for :admins
end
class CreateVotes < ActiveRecord::Migration
def change
create_table :votes do |t|
t.references :user_id, index: true, foreign_key: true
t.references :candidate_id, index: true, foreign_key: true
t.references :user, index: true, foreign_key: true
t.references :candidate, index: true, foreign_key: true
t.text :comments
t.timestamps null: false
......
......@@ -73,14 +73,14 @@ ActiveRecord::Schema.define(version: 20160712083445) do
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_id"
t.integer "candidate_id_id"
t.integer "user_id"
t.integer "candidate_id"
t.text "comments"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "votes", ["candidate_id_id"], name: "index_votes_on_candidate_id_id"
add_index "votes", ["user_id_id"], name: "index_votes_on_user_id_id"
add_index "votes", ["candidate_id"], name: "index_votes_on_candidate_id"
add_index "votes", ["user_id"], name: "index_votes_on_user_id"
end
require 'test_helper'
class VotesControllerTest < ActionController::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