Allow deleting candidates

This commit is contained in:
ricola 2025-04-06 17:04:31 -06:00
parent 15daf75c38
commit 4462159b93
2 changed files with 13 additions and 1 deletions

View file

@ -17,6 +17,9 @@
<% @vote.candidates.each do |candidate| %> <% @vote.candidates.each do |candidate| %>
<h3><%= candidate.name %></h3> <h3><%= candidate.name %></h3>
<p><%= candidate.description %></p> <p><%= candidate.description %></p>
<form action="/votes/<%= @vote.secure_id %>/candidates/<%= candidate.id %>/delete" method="post">
<button type="submit">Delete candidate</button>
</form>
<% end %> <% end %>
<h3>Add candidate</h3> <h3>Add candidate</h3>

11
vote.rb
View file

@ -15,7 +15,7 @@ end
class Candidate < ActiveRecord::Base class Candidate < ActiveRecord::Base
belongs_to :vote belongs_to :vote
has_many :ratings has_many :ratings, dependent: :destroy
def mj def mj
return MajorityJudgment.new(self.ratings.collect {|r| r.value }) return MajorityJudgment.new(self.ratings.collect {|r| r.value })
@ -151,6 +151,15 @@ post '/votes/:id/candidates' do
redirect '/votes/' + @vote.secure_id redirect '/votes/' + @vote.secure_id
end 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 post '/votes/:id/open' do
redirect '/login' unless current_user redirect '/login' unless current_user
@vote = Vote.find_by(secure_id: params[:id]) @vote = Vote.find_by(secure_id: params[:id])