diff --git a/views/votes_edit.erb b/views/votes_edit.erb
index a54407c..a9414f1 100644
--- a/views/votes_edit.erb
+++ b/views/votes_edit.erb
@@ -17,6 +17,9 @@
<% @vote.candidates.each do |candidate| %>
<%= candidate.name %>
<%= candidate.description %>
+
<% end %>
Add candidate
diff --git a/vote.rb b/vote.rb
index 85f8fa4..994e283 100644
--- a/vote.rb
+++ b/vote.rb
@@ -15,7 +15,7 @@ end
class Candidate < ActiveRecord::Base
belongs_to :vote
- has_many :ratings
+ has_many :ratings, dependent: :destroy
def mj
return MajorityJudgment.new(self.ratings.collect {|r| r.value })
@@ -151,6 +151,15 @@ post '/votes/:id/candidates' do
redirect '/votes/' + @vote.secure_id
end
+post '/votes/:id/candidates/:cid/delete' do
+ redirect '/login' unless current_user
+ @vote = Vote.find_by(secure_id: params[:id])
+ redirect '/votes/' + @vote.secure_id unless @vote.state == "draft" and @vote.users.exists?(current_user.id)
+ @candidate = Candidate.find(params[:cid])
+ @candidate.destroy
+ redirect '/votes/' + @vote.secure_id
+end
+
post '/votes/:id/open' do
redirect '/login' unless current_user
@vote = Vote.find_by(secure_id: params[:id])