Allow delete vote
This commit is contained in:
parent
b254497ddc
commit
ac32ce243a
2 changed files with 14 additions and 3 deletions
|
|
@ -56,3 +56,6 @@
|
||||||
<form action="/votes/<%= @vote.secure_id %>/open" method="post">
|
<form action="/votes/<%= @vote.secure_id %>/open" method="post">
|
||||||
<button type="submit">Open vote to participants</button>
|
<button type="submit">Open vote to participants</button>
|
||||||
</form>
|
</form>
|
||||||
|
<form action="/votes/<%= @vote.secure_id %>/delete" method="post">
|
||||||
|
<button type="submit">Delete vote</button>
|
||||||
|
</form>
|
||||||
|
|
|
||||||
14
vote.rb
14
vote.rb
|
|
@ -6,9 +6,9 @@ require 'securerandom'
|
||||||
require_relative 'mj'
|
require_relative 'mj'
|
||||||
|
|
||||||
class Vote < ActiveRecord::Base
|
class Vote < ActiveRecord::Base
|
||||||
has_many :candidates
|
has_many :candidates, dependent: :destroy
|
||||||
has_many :ratings
|
has_many :ratings, dependent: :destroy
|
||||||
has_many :organizers
|
has_many :organizers, dependent: :destroy
|
||||||
has_many :users, through: :organizers
|
has_many :users, through: :organizers
|
||||||
validates :state, inclusion: { in: ["draft", "open", "closed"] }
|
validates :state, inclusion: { in: ["draft", "open", "closed"] }
|
||||||
end
|
end
|
||||||
|
|
@ -207,6 +207,14 @@ post '/votes/:id/organizers' do
|
||||||
redirect '/votes/' + @vote.secure_id
|
redirect '/votes/' + @vote.secure_id
|
||||||
end
|
end
|
||||||
|
|
||||||
|
post '/votes/:id/delete' do
|
||||||
|
redirect '/login' unless current_user
|
||||||
|
@vote = Vote.find_by(secure_id: params[:id])
|
||||||
|
redirect '/votes/' + @vote.secure_id unless @vote.users.exists?(current_user.id)
|
||||||
|
@vote.destroy
|
||||||
|
redirect '/'
|
||||||
|
end
|
||||||
|
|
||||||
helpers do
|
helpers do
|
||||||
def current_user
|
def current_user
|
||||||
if session[:user_id]
|
if session[:user_id]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue