Commit 5e8443b6 authored by Bianca Tarun's avatar Bianca Tarun

edited votes controller

parent 2cc6b35b
class VotesController < ApplicationController
before_action :authenticate_user!
def index
@positions = Position.all
@votes = current_user.votes
@candidates = Candidate.all
@candidates_by_position = Candidate.all.group_by(&:position_id)
end
def new
if Vote.exists?(user: current_user)
redirect_to root_path, alert: "You have already voted."
else
@user = current_user
@user.votes.build
@user = current_user.votes.build
@positions = Position.all
render "votes/new.html.erb"
end
end
def checkout
if current_user.update(vote_params)
redirect_to root_path
......@@ -26,17 +24,12 @@ class VotesController < ApplicationController
end
def profile
if user_signed_in?
@user = current_user
@votes = Vote.where(user_id: (@user.id))
else
redirect_to root_path
end
@votes = current_user.votes
end
private
def vote_params
params.require(:vote).permit!
params.require(:user).permit!
end
end
\ No newline at end of file
......@@ -5,4 +5,5 @@ class Vote < ActiveRecord::Base
def to_s
"#{candidate.first_name} #{candidate.last_name}"
end
accepts_nested_attributes_for :votes
end
......@@ -23,7 +23,7 @@
<%= 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
<%= link_to "Vote Record", votes_index_path %>
<% end %>
<script>
......
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