Commit 343c4ce8 authored by royce-matthew's avatar royce-matthew

Added Associations between User and Votes and also added controllers and views...

Added Associations between User and Votes and also added controllers and views for voting module - Royce and Travis
parent 90e91522
......@@ -6,12 +6,5 @@ class PagesController < ApplicationController
render "pages/index.html.erb"
end
def profile
before_action :authenticate_user!
@votes = Vote.all
@candidates = Candidate.all
@positions = Position.all
render "pages/index.html.erb"
end
end
\ No newline at end of file
module Voting
class VotesController < ApplicationController
before_action :authenticate_user!
def index
@positions = Position.all
@candidates = Candidate.all
@votes = Vote.all
render "voting/votes/index.html.erb"
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(post_params())
redirect_to admin_post_path(@post.id)
else
render "admin/posts/edit.html.erb"
end
end
def new
@votes= Vote.all
@pos = params[:id]
@vote = Vote.new
render "voting/votes/new.html.erb"
end
def create
@vote = current_user.votes.build(vote_params())
if @vote.save
redirect_to voting_votes_path(@vote.id)
else
render "voting/votes/new.html.erb"
end
end
def vote_params
params.require(:vote).permit!
end
end
end
\ No newline at end of file
class User < ApplicationRecord
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :votes
def list_of_positions
x = "hello"
end
end
class Vote < ApplicationRecord
class Vote < ActiveRecord::Base
belongs_to :user
belongs_to :candidate
validates :user_id, :uniqueness => { scope: [:candidate],
:message => "Users may only write one vote per candidate" }
end
<%
if @pos == nil
@pos = 0..99
else
end
%>
<%= simple_form_for([:voting, @vote]) do |f| %>
I wanna vote for
<%= f.association :candidate, collection: Candidate.where(position_id:@pos), label_method: :full_name, value_method: :id %>
<%= f.input :comments%>
<%= f.submit %>
<% end %>
<h1>Admin Edit Post</h1>
<%= render partial: "form" %>
<%= link_to "Back to Posts", admin_posts_path %>
\ No newline at end of file
<h1>Votes</h1>
<!--
<%= link_to("New Vote", new_voting_vote_path) %>
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
-->
<br>
<td>Hello <%= current_user.email %></td>
<br>
<br>
<ul>
<%@positions.each do |p|%>
<li> has user already voted for <%=p.name%>?<br>
<%voted_for = false%>
<% p.candidates.each do |c|%>
<%c.votes.each do |v|%>
<%if v.user_id == current_user.id%>
<%voted_for = true%>
<% end %>
<%end%>
<% end %>
</ol>
<%if !voted_for%>
<%=link_to "Vote for a #{p.name}", "/voting/new/#{p.id}" %>
<%else%>
You have already Voted for a <%= p.name%>
<%end%>
</li>
<%end%>
</ul>
<hr>
<br>
\ No newline at end of file
<h1>Vote</h1>
<%= render partial: "form" %>
<%= link_to "Back to Botes", voting_votes_path %>
<h1><%= @post.title %></h1>
<h3><%= @post.published_at %></h3>
<p>
<%= @post.content %>
</p>
<hr>
<%= link_to "Back to Posts", admin_posts_path %>
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