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 class CandidatesController < ApplicationController
before_action :authenticate_user!, except: [:index] #or except before_action :authenticate_admin!, except: [:index]
def index def index
@candidates = Candidate.all @candidates = Candidate.all
end end
def vote
end
def new def new
@candidate = Candidate.new @candidate = Candidate.new
end end
...@@ -16,7 +13,7 @@ class CandidatesController < ApplicationController ...@@ -16,7 +13,7 @@ class CandidatesController < ApplicationController
@candidate = Candidate.new(candidate_params) @candidate = Candidate.new(candidate_params)
if @candidate.save if @candidate.save
redirect_to candidate_path(@candidate), notice: "User was successfully created" redirect_to candidates_path, notice: "User was successfully created"
else else
render 'new' render 'new'
end end
...@@ -26,7 +23,7 @@ class CandidatesController < ApplicationController ...@@ -26,7 +23,7 @@ class CandidatesController < ApplicationController
@candidate = Candidate.find(params[:id]) @candidate = Candidate.find(params[:id])
@candidate.destroy() @candidate.destroy()
redirect_to candidate_path redirect_to candidates_path
end end
...@@ -35,6 +32,13 @@ class CandidatesController < ApplicationController ...@@ -35,6 +32,13 @@ class CandidatesController < ApplicationController
end end
def update def update
@candidate = Candidate.find(params[:id])
if @candidate.update(candidate_params)
redirect_to candidates_path
else
render 'edit'
end
end end
def show 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 ...@@ -4,4 +4,6 @@ class User < ActiveRecord::Base
has_many :votes has_many :votes
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable :recoverable, :rememberable, :trackable, :validatable
end end
class Vote < ActiveRecord::Base class Vote < ActiveRecord::Base
belongs_to :user_id belongs_to :user
belongs_to :candidate_id belongs_to :candidate
validates :candidate, presence: true, uniqueness: true
end end
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<%= c.input :nickname %> <%= c.input :nickname %>
<%= c.input :slogan, as: :text %> <%= c.input :slogan, as: :text %>
<%= c.association :position %> <%= c.association :position %>
<%= c.submit %> <%= c.button :submit, "Submit", class: "btn btn-success" %>
<%= link_to 'Cancel', candidates_path, class: "btn btn-danger" %>
<% end %> <% end %>
<%= link_to 'Cancel', candidates_path, class: "btn btn-danger" %>
\ No newline at end of file
<div class = "container"> <div class = "container">
<h1>Edit Candidate</h1> <h1>Edit Candidate</h1>
<%= render 'form' %> <%= render 'form' %>
</div> </div>
\ No newline at end of file
<div class = "container"> <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> <h4> <%= p.name %> </h4>
<table class = "table"> <table class = "table">
<thead> <thead>
<th>First Name</th> <th>First Name</th>
<th>Last Name</th> <th>Last Name</th>
<th>Nickname</th> <th>Nickname</th>
<% if !admin_signed_in? %>
<th>Votes</th> <th>Votes</th>
<% end %>
<% if admin_signed_in? %>
<th>Slogan</th> <th>Slogan</th>
<% end %>
</thead> </thead>
<tbody> <tbody>
<% can.each do |c| %> <% can.each do |c| %>
<tr> <tr>
<td> <%= c.first_name %> </td> <td> <%= c.first_name %> </td>
<td> <%= c.last_name %> </td> <td> <%= c.last_name %> </td>
<td> <%= c.nickname %></td> <td> <%= c.nickname %></td>
<% if !admin_signed_in? %> <td> <%= c.votes.count%></td>
<td> <%= %></td>
<% end %>
<% if admin_signed_in? %>
<td> <%= c.slogan %> <td> <%= c.slogan %>
<td> <%= link_to 'Show', show_candidates_path(c), class: "btn btn-primary" %></td> <td> <%= link_to 'Show', candidate_path(c), class: "btn btn-primary" %></td>
<td> <%= link_to 'Edit', edit_candidates_path(c), class: "btn btn-success" %></td> <td> <%= link_to 'Edit', edit_candidate_path(c), class: "btn btn-success" %></td>
<td> <a href="/candidates/delete?id=<%= c.id %>"><button class="btn btn-danger">Delete</button></a></td> <td> <%= link_to 'Delete', candidate_path(c), method: :delete, data: { confirm: "Are you sure you want to delete?" }, class: "btn btn-danger" %> </td>
<% end %>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table> </table>
<% end %>
<% end %> <%= link_to 'New Candidate', new_candidate_path, class: "btn btn-primary" %>
<% 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 %>
</div> </div>
\ No newline at end of file
<h1>New Candidate</h1> <div class = "container">
<h1>New Candidate</h1>
<%= render 'form' %>
<!--<%= form_for(@candidate) do |u| %> </div>
<% if @candidate.errors.any? %> \ No newline at end of file
<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
<h1> <%= @candidate.first_name %> <%= @candidate.last_name %></h1> <div class = "container">
<h1> <%= @candidate.first_name %> <%= @candidate.last_name %></h1>
<table> <table class = "table">
<thead> <thead>
<th>Nickname</th> <th>Nickname</th>
<th>Slogan</th> <th>Slogan</th>
<th>Votes</th> <th>Votes</th>
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td> <%= @candidate.nickname %></td> <td> <%= @candidate.nickname %></td>
<td> <%= @candidate.slogan %></td> <td> <%= @candidate.slogan %></td>
</tr> </tr>
</tbody> </tbody>
</table>
</table> <%= link_to 'Back', candidates_path, class: "btn btn-primary" %>
</div>
<%= link_to 'Back', candidates_path, class: "btn btn-primary" %> \ No newline at end of file
\ No newline at end of file
...@@ -15,7 +15,8 @@ ...@@ -15,7 +15,8 @@
</div> </div>
<ul class="nav navbar-nav"> <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> </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> <div class = "container">
<%= form_for(@position, url: update_position_path(@position)) do |p| %> <h1>Edit Position</h1>s
<p>
<%= p.label "Name" %>
<%= p.text_field :name %>
</p>
<p> <%= render 'form' %>
<%= p.label "Number of Winners" %> </div>
<%= p.text_field :number_of_winners %> \ No newline at end of file
</p>
<button class = "btn btn-success">Edit position <% p.submit %></button>
<% end %>
<%= link_to 'Cancel', positions_path, class: "btn btn-danger" %>
<h1> List of Positions</h1> <div class = "container">
<table class = "table"> <h1> List of Positions</h1>
<table class = "table">
<thead> <thead>
<th>Name</th> <th>Name</th>
<th>Number of Winners</th> <th>Number of Winners</th>
</thead> </thead>
<tbody> <tbody>
<% @positions.each do |c| %> <% @positions.each do |c| %>
<tr> <tr>
<td> <%= c.name %> </td> <td> <%= c.name %> </td>
<td> <%= link_to 'Show', show_position_path(c) %> </td> <td> <%= c.number_of_winners %></td>
<td> <%= link_to 'Edit', edit_position_path(c) %> </td> <td> <%= link_to 'Show', position_path(c), class: "btn btn-primary" %> </td>
<td> <%= link_to 'Delete', delete_position_path(c) %> </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> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table>
</table> <%= link_to 'Add new position', new_position_path, class: "btn btn-primary" %>
</div>
<%= link_to 'Add new position', new_positions_path, class: "btn btn-primary" %> \ No newline at end of file
\ No newline at end of file
<h1>New Position</h1> <div class = "container">
<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" %>
<%= 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> <thead>
<th>First Name</th> <th>First Name</th>
<th>Last Name</th> <th>Last Name</th>
<th>Nickname</th> <th>Nickname</th>
<th>Slogan</th> <th>Slogan</th>
<th>Votes</th>
</thead> </thead>
<tbody> <tbody>
<% @position.candidate.each do |c| %> <% @position.candidates.each do |c| %>
<tr> <tr>
<td> <%= c.first_name %> </td> <td> <%= c.first_name %> </td>
<td> <%= c.last_name %> </td> <td> <%= c.last_name %> </td>
<td> <%= c.nickname %></td> <td> <%= c.nickname %></td>
<td> <%= c.slogan %></td> <td> <%= c.slogan %></td>
<td> <%= c.votes.count %></td>
</tr> </tr>
<% end %> <% end %>
</tbody> </tbody>
</table>
</table> <%= link_to 'Back', positions_path, class: "btn btn-primary" %>
</div>
<%= link_to 'Back', positions_path %> \ No newline at end of file
\ 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 Rails.application.routes.draw do
resources :candidates
resources :positions
devise_for :users get '/votes/index', to: 'votes#index', as: :votes
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
post '/candidates', to: 'candidates#create', as: :candidate get 'voting', to: 'votes#voting', as: :voting
root 'votes#index'
get 'positions', to:'positions#index', as: :positions devise_for :users
devise_for :admins
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
end end
class CreateVotes < ActiveRecord::Migration class CreateVotes < ActiveRecord::Migration
def change def change
create_table :votes do |t| create_table :votes do |t|
t.references :user_id, index: true, foreign_key: true t.references :user, index: true, foreign_key: true
t.references :candidate_id, index: true, foreign_key: true t.references :candidate, index: true, foreign_key: true
t.text :comments t.text :comments
t.timestamps null: false t.timestamps null: false
......
...@@ -73,14 +73,14 @@ ActiveRecord::Schema.define(version: 20160712083445) do ...@@ -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 add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
create_table "votes", force: :cascade do |t| create_table "votes", force: :cascade do |t|
t.integer "user_id_id" t.integer "user_id"
t.integer "candidate_id_id" t.integer "candidate_id"
t.text "comments" t.text "comments"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
add_index "votes", ["candidate_id_id"], name: "index_votes_on_candidate_id_id" add_index "votes", ["candidate_id"], name: "index_votes_on_candidate_id"
add_index "votes", ["user_id_id"], name: "index_votes_on_user_id_id" add_index "votes", ["user_id"], name: "index_votes_on_user_id"
end 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