Compare commits

...

2 Commits

Author SHA1 Message Date
9f42deee95 Add references between projects and todos
Some checks failed
CI / scan_ruby (push) Has been cancelled
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / system-test (push) Has been cancelled
2025-10-27 13:48:15 +01:00
871377a90b Add fields to models 2025-10-27 13:30:39 +01:00
10 changed files with 61 additions and 2 deletions

View File

@@ -65,6 +65,6 @@ class TodosController < ApplicationController
# Only allow a list of trusted parameters through.
def todo_params
params.expect(todo: [ :name, :description ])
params.expect(todo: [ :name, :description, :completed, :priority, :project_id ])
end
end

View File

@@ -1,3 +1,4 @@
class Project < ApplicationRecord
has_many :todos, dependent: :destroy
validates :name, presence: true
end

View File

@@ -1,2 +1,3 @@
class Todo < ApplicationRecord
belongs_to :project
end

View File

@@ -1,4 +1,9 @@
<h1><%= @project.name %></h1>
<% @project.todos.each do |todo| %>
<li><%= link_to todo.name, todo_path(todo) %></li>
<% end %>
<%= link_to "Edit", edit_project_path %>
<%= button_to "Delete", project_path, method: :delete %>
<%= link_to "Back", projects_path %>

View File

@@ -21,6 +21,21 @@
<%= form.textarea :description %>
</div>
<div>
<%= form.label :completed %>
<%= form.check_box :completed %>
</div>
<div>
<%= form.label :priority %>
<%= form.number_field :priority %>
</div>
<div>
<%= form.label :project_id %>
<%= form.collection_select :project_id, Project.all, :id, :name, prompt: "Select a project" %>
</div>
<div>
<%= form.submit %>
</div>

View File

@@ -9,4 +9,18 @@
<%= todo.description %>
</div>
<div>
<strong>Completed:</strong>
<%= todo.completed %>
</div>
<div>
<strong>Priority:</strong>
<%= todo.priority %>
</div>
<div>
<strong>Project:</strong>
<%= link_to todo.project.name, project_path(todo.project) %>
</div>
</div>

View File

@@ -0,0 +1,6 @@
class AddCompletedAndPriorityToTodos < ActiveRecord::Migration[8.1]
def change
add_column :todos, :completed, :boolean, default: false, null: false
add_column :todos, :priority, :integer, default: 1, null: false
end
end

View File

@@ -0,0 +1,5 @@
class AddActiveToProjects < ActiveRecord::Migration[8.1]
def change
add_column :projects, :active, :boolean, default: true, null: false
end
end

View File

@@ -0,0 +1,5 @@
class AddProjectIdToTodos < ActiveRecord::Migration[8.1]
def change
add_reference :todos, :project, null: false, foreign_key: true
end
end

9
db/schema.rb generated
View File

@@ -10,17 +10,24 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.1].define(version: 2025_10_27_095143) do
ActiveRecord::Schema[8.1].define(version: 2025_10_27_123148) do
create_table "projects", force: :cascade do |t|
t.boolean "active", default: true, null: false
t.datetime "created_at", null: false
t.string "name"
t.datetime "updated_at", null: false
end
create_table "todos", force: :cascade do |t|
t.boolean "completed", default: false, null: false
t.datetime "created_at", null: false
t.text "description"
t.string "name"
t.integer "priority", default: 1, null: false
t.integer "project_id", null: false
t.datetime "updated_at", null: false
t.index ["project_id"], name: "index_todos_on_project_id"
end
add_foreign_key "todos", "projects"
end