Create model for ratings
This commit is contained in:
parent
6682c8c300
commit
4b5685124c
4 changed files with 44 additions and 2 deletions
10
db/migrate/20250321235135_create_ratings.rb
Normal file
10
db/migrate/20250321235135_create_ratings.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
class CreateRatings < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
create_table :ratings do |t|
|
||||
t.belongs_to :user
|
||||
t.belongs_to :candidate
|
||||
t.integer :value
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
12
db/schema.rb
12
db/schema.rb
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[7.2].define(version: 2025_03_21_234644) do
|
||||
ActiveRecord::Schema[7.2].define(version: 2025_03_21_235135) do
|
||||
create_table "candidates", force: :cascade do |t|
|
||||
t.integer "vote_id"
|
||||
t.string "name"
|
||||
|
|
@ -20,6 +20,16 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_21_234644) do
|
|||
t.index ["vote_id"], name: "index_candidates_on_vote_id"
|
||||
end
|
||||
|
||||
create_table "ratings", force: :cascade do |t|
|
||||
t.integer "user_id"
|
||||
t.integer "candidate_id"
|
||||
t.integer "value"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["candidate_id"], name: "index_ratings_on_candidate_id"
|
||||
t.index ["user_id"], name: "index_ratings_on_user_id"
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
t.string "email"
|
||||
t.string "password"
|
||||
|
|
|
|||
|
|
@ -4,16 +4,28 @@
|
|||
|
||||
<p>Hello, <%= current_user.email %>.</p>
|
||||
|
||||
<h2>Candidates</h2>
|
||||
<h2>Your ratings</h2>
|
||||
|
||||
<form action="/votes/<%= @vote.secure_id %>/ratings" method="post">
|
||||
<ul>
|
||||
<% @vote.candidates.each do |candidate| %>
|
||||
<li>
|
||||
<p><%= candidate.name %></p>
|
||||
<p><%= candidate.description %></p>
|
||||
<ol>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-7" value="7" <% if @ratings[candidate] and @ratings[candidate].value == 7 %>checked<% end %>><label for="<%= candidate.id %>-7">Excellent</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-6" value="6" <% if @ratings[candidate] and @ratings[candidate].value == 6 %>checked<% end %>><label for="<%= candidate.id %>-6">Very good</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-5" value="5" <% if @ratings[candidate] and @ratings[candidate].value == 5 %>checked<% end %>><label for="<%= candidate.id %>-5">Good</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-4" value="4" <% if @ratings[candidate] and @ratings[candidate].value == 4 %>checked<% end %>><label for="<%= candidate.id %>-4">Mediocre</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-3" value="3" <% if @ratings[candidate] and @ratings[candidate].value == 3 %>checked<% end %>><label for="<%= candidate.id %>-3">Bad</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-2" value="2" <% if @ratings[candidate] and @ratings[candidate].value == 2 %>checked<% end %>><label for="<%= candidate.id %>-2">Very bad</label>
|
||||
<input type="radio" name="<%= candidate.id %>" id="<%= candidate.id %>-1" value="1" <% if @ratings[candidate] and @ratings[candidate].value == 1 %>checked<% end %>><label for="<%= candidate.id %>-1">Awful</label>
|
||||
</ol>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<button type="submit">Save ratings</button>
|
||||
</form>
|
||||
|
||||
<h2>New candidate</h2>
|
||||
|
||||
|
|
|
|||
10
vote.rb
10
vote.rb
|
|
@ -16,6 +16,12 @@ class Candidate < ActiveRecord::Base
|
|||
end
|
||||
|
||||
class User < ActiveRecord::Base
|
||||
has_many :ratings
|
||||
end
|
||||
|
||||
class Rating < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
belongs_to :candidate
|
||||
end
|
||||
|
||||
def hash_password(password)
|
||||
|
|
@ -72,6 +78,10 @@ end
|
|||
get '/votes/:id' do
|
||||
redirect '/login' unless current_user
|
||||
@vote = Vote.find(params[:id])
|
||||
@ratings = {}
|
||||
@vote.candidates.each do |candidate|
|
||||
@ratings[candidate] = Rating.where("user_id = ? and candidate_id = ?", current_user.id, candidate.id)[0]
|
||||
end
|
||||
erb :votes_show
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue