Display all ratings for a vote
This commit is contained in:
parent
5096134d2e
commit
46c8745a9e
3 changed files with 40 additions and 19 deletions
18
vote.rb
18
vote.rb
|
|
@ -7,12 +7,13 @@ require 'securerandom'
|
|||
#set :database, 'sqlite3:db/vote.sqlite3'
|
||||
|
||||
class Vote < ActiveRecord::Base
|
||||
self.primary_key = "secure_id"
|
||||
has_many :candidates
|
||||
has_many :ratings
|
||||
end
|
||||
|
||||
class Candidate < ActiveRecord::Base
|
||||
belongs_to :vote
|
||||
has_many :ratings
|
||||
end
|
||||
|
||||
class User < ActiveRecord::Base
|
||||
|
|
@ -20,6 +21,7 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
|
||||
class Rating < ActiveRecord::Base
|
||||
belongs_to :vote
|
||||
belongs_to :user
|
||||
belongs_to :candidate
|
||||
end
|
||||
|
|
@ -77,11 +79,7 @@ end
|
|||
|
||||
get '/votes/:id' do
|
||||
redirect '/login' unless current_user
|
||||
@vote = Vote.find(params[:id])
|
||||
@ratings = {}
|
||||
@vote.candidates.each do |candidate|
|
||||
@ratings[candidate] = Rating.where("user_id = ? and candidate_id = ?", current_user.id, candidate.id)[0]
|
||||
end
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
erb :votes_show
|
||||
end
|
||||
|
||||
|
|
@ -93,7 +91,7 @@ post '/votes' do
|
|||
end
|
||||
|
||||
post '/votes/:id/candidates' do
|
||||
@vote = Vote.find(params[:id])
|
||||
@vote = Vote.find_by(secure_id: params[:id])
|
||||
@candidate = Candidate.new(name: params[:name],
|
||||
description: params[:description])
|
||||
@candidate.vote = @vote
|
||||
|
|
@ -103,11 +101,9 @@ end
|
|||
|
||||
post '/votes/:id/ratings' do
|
||||
redirect '/login' unless current_user
|
||||
vote = Vote.find(params[:id])
|
||||
vote = Vote.find_by(secure_id: params[:id])
|
||||
vote.candidates.each do |candidate|
|
||||
rating = Rating.find_by(user: current_user)
|
||||
rating = Rating.find_by(candidate: candidate)
|
||||
rating = Rating.find_or_initialize_by(user: current_user, candidate: candidate)
|
||||
rating = Rating.find_or_initialize_by(vote: vote, user: current_user, candidate: candidate)
|
||||
rating.value = params[candidate.id.to_s]
|
||||
rating.save
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue