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

edited votes controller

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