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
|
|
@ -5,7 +5,7 @@ class CreateVotes < ActiveRecord::Migration[7.2]
|
||||||
t.string :title
|
t.string :title
|
||||||
t.text :description
|
t.text :description
|
||||||
t.datetime :expire_on
|
t.datetime :expire_on
|
||||||
t.timestamps null: false
|
t.timestamps
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
10
db/migrate/20250321224552_create_candidates.rb
Normal file
10
db/migrate/20250321224552_create_candidates.rb
Normal file
|
|
@ -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
|
||||||
11
db/schema.rb
11
db/schema.rb
|
|
@ -10,7 +10,16 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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|
|
create_table "votes", force: :cascade do |t|
|
||||||
t.string "secure_id"
|
t.string "secure_id"
|
||||||
t.string "title"
|
t.string "title"
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,13 @@
|
||||||
|
|
||||||
<h2>Votes</h2>
|
<h2>Votes</h2>
|
||||||
|
|
||||||
|
<ul>
|
||||||
<% @votes.each do |vote| %>
|
<% @votes.each do |vote| %>
|
||||||
<li>
|
<li>
|
||||||
<a href="/votes/<%= vote.secure_id %>"><%= vote.title %></a>
|
<a href="/votes/<%= vote.secure_id %>"><%= vote.title %></a>
|
||||||
</li>
|
</li>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
|
||||||
<form action="/votes/new" method="get">
|
<form action="/votes/new" method="get">
|
||||||
<button type="submit">Create new vote</button>
|
<button type="submit">Create new vote</button>
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,28 @@
|
||||||
<h1><%= @vote.title %></h1>
|
<h1><%= @vote.title %></h1>
|
||||||
|
|
||||||
<p><%= @vote.description %></p>
|
<p><%= @vote.description %></p>
|
||||||
|
|
||||||
|
<h2>Candidates</h2>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<% @vote.candidates.each do |candidate| %>
|
||||||
|
<li>
|
||||||
|
<p><%= candidate.name %></p>
|
||||||
|
<p><%= candidate.description %></p>
|
||||||
|
</li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<h2>New candidate</h2>
|
||||||
|
|
||||||
|
<form action="/votes/<%= @vote.secure_id %>/candidates" method="post">
|
||||||
|
<p>
|
||||||
|
<label for="name">Name</label>
|
||||||
|
<input type="text" name="name">
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<label for="description">Description</label>
|
||||||
|
<input type="text" name="description">
|
||||||
|
</p>
|
||||||
|
<button type="submit">Add new candidate</button>
|
||||||
|
</form>
|
||||||
|
|
|
||||||
19
vote.rb
19
vote.rb
|
|
@ -8,6 +8,11 @@ require 'securerandom'
|
||||||
|
|
||||||
class Vote < ActiveRecord::Base
|
class Vote < ActiveRecord::Base
|
||||||
self.primary_key = "secure_id"
|
self.primary_key = "secure_id"
|
||||||
|
has_many :candidates
|
||||||
|
end
|
||||||
|
|
||||||
|
class Candidate < ActiveRecord::Base
|
||||||
|
belongs_to :vote
|
||||||
end
|
end
|
||||||
|
|
||||||
def hash_password(password)
|
def hash_password(password)
|
||||||
|
|
@ -64,11 +69,19 @@ get '/votes/:id' do
|
||||||
end
|
end
|
||||||
|
|
||||||
post '/votes' do
|
post '/votes' do
|
||||||
secure_id = SecureRandom.hex(8)
|
@vote = Vote.create(secure_id: SecureRandom.hex(8),
|
||||||
@vote = Vote.create(secure_id: secure_id,
|
|
||||||
title: params[:title],
|
title: params[:title],
|
||||||
description: params[:description])
|
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
|
end
|
||||||
|
|
||||||
helpers do
|
helpers do
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue