Create model for candidates with name and description
This commit is contained in:
parent
f7f1807839
commit
d6500e013d
6 changed files with 64 additions and 5 deletions
19
vote.rb
19
vote.rb
|
|
@ -8,6 +8,11 @@ require 'securerandom'
|
|||
|
||||
class Vote < ActiveRecord::Base
|
||||
self.primary_key = "secure_id"
|
||||
has_many :candidates
|
||||
end
|
||||
|
||||
class Candidate < ActiveRecord::Base
|
||||
belongs_to :vote
|
||||
end
|
||||
|
||||
def hash_password(password)
|
||||
|
|
@ -64,11 +69,19 @@ get '/votes/:id' do
|
|||
end
|
||||
|
||||
post '/votes' do
|
||||
secure_id = SecureRandom.hex(8)
|
||||
@vote = Vote.create(secure_id: secure_id,
|
||||
@vote = Vote.create(secure_id: SecureRandom.hex(8),
|
||||
title: params[:title],
|
||||
description: params[:description])
|
||||
redirect '/votes/' + secure_id
|
||||
redirect '/votes/' + @vote.secure_id
|
||||
end
|
||||
|
||||
post '/votes/:id/candidates' do
|
||||
@vote = Vote.find(params[:id])
|
||||
@candidate = Candidate.new(name: params[:name],
|
||||
description: params[:description])
|
||||
@candidate.vote = @vote
|
||||
@candidate.save
|
||||
redirect '/votes/' + @vote.secure_id
|
||||
end
|
||||
|
||||
helpers do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue