From d6500e013d0316ce6c70c70f1bbfaaf97437778f Mon Sep 17 00:00:00 2001 From: ricola Date: Sun, 6 Apr 2025 17:04:31 -0600 Subject: [PATCH] Create model for candidates with name and description --- db/migrate/20250321213011_create_votes.rb | 2 +- .../20250321224552_create_candidates.rb | 10 ++++++++ db/schema.rb | 11 +++++++- views/home.erb | 2 ++ views/votes_show.erb | 25 +++++++++++++++++++ vote.rb | 19 +++++++++++--- 6 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20250321224552_create_candidates.rb diff --git a/db/migrate/20250321213011_create_votes.rb b/db/migrate/20250321213011_create_votes.rb index 321e58f..70f6281 100644 --- a/db/migrate/20250321213011_create_votes.rb +++ b/db/migrate/20250321213011_create_votes.rb @@ -5,7 +5,7 @@ class CreateVotes < ActiveRecord::Migration[7.2] t.string :title t.text :description t.datetime :expire_on - t.timestamps null: false + t.timestamps end end end diff --git a/db/migrate/20250321224552_create_candidates.rb b/db/migrate/20250321224552_create_candidates.rb new file mode 100644 index 0000000..e69b0d2 --- /dev/null +++ b/db/migrate/20250321224552_create_candidates.rb @@ -0,0 +1,10 @@ +class CreateCandidates < ActiveRecord::Migration[7.2] + def change + create_table :candidates do |t| + t.belongs_to :vote + t.string :name + t.text :description + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 8245e87..ca192cc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,16 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2025_03_21_213011) do +ActiveRecord::Schema[7.2].define(version: 2025_03_21_224552) do + create_table "candidates", force: :cascade do |t| + t.integer "vote_id" + t.string "name" + t.text "description" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["vote_id"], name: "index_candidates_on_vote_id" + end + create_table "votes", force: :cascade do |t| t.string "secure_id" t.string "title" diff --git a/views/home.erb b/views/home.erb index 962ec6b..520e4db 100644 --- a/views/home.erb +++ b/views/home.erb @@ -4,11 +4,13 @@

Votes

+
diff --git a/views/votes_show.erb b/views/votes_show.erb index 480b3f7..56cbde3 100644 --- a/views/votes_show.erb +++ b/views/votes_show.erb @@ -1,3 +1,28 @@

<%= @vote.title %>

<%= @vote.description %>

+ +

Candidates

+ + + +

New candidate

+ + +

+ + +

+

+ + +

+ +
diff --git a/vote.rb b/vote.rb index ead780e..7322445 100644 --- a/vote.rb +++ b/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