diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index a3f6b2c..31a9ca8 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -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 diff --git a/app/models/project.rb b/app/models/project.rb index 3f00e16..034ff7b 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,3 +1,4 @@ class Project < ApplicationRecord + has_many :todos, dependent: :destroy validates :name, presence: true end diff --git a/app/models/todo.rb b/app/models/todo.rb index e7adee6..19af494 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -1,2 +1,3 @@ class Todo < ApplicationRecord + belongs_to :project end diff --git a/app/views/projects/show.html.erb b/app/views/projects/show.html.erb index d888e38..af03c54 100644 --- a/app/views/projects/show.html.erb +++ b/app/views/projects/show.html.erb @@ -1,4 +1,9 @@

<%= @project.name %>

+ +<% @project.todos.each do |todo| %> +
  • <%= link_to todo.name, todo_path(todo) %>
  • +<% end %> + <%= link_to "Edit", edit_project_path %> <%= button_to "Delete", project_path, method: :delete %> <%= link_to "Back", projects_path %> \ No newline at end of file diff --git a/app/views/todos/_form.html.erb b/app/views/todos/_form.html.erb index 011c569..2e437c1 100644 --- a/app/views/todos/_form.html.erb +++ b/app/views/todos/_form.html.erb @@ -21,6 +21,21 @@ <%= form.textarea :description %> +
    + <%= form.label :completed %> + <%= form.check_box :completed %> +
    + +
    + <%= form.label :priority %> + <%= form.number_field :priority %> +
    + +
    + <%= form.label :project_id %> + <%= form.collection_select :project_id, Project.all, :id, :name, prompt: "Select a project" %> +
    +
    <%= form.submit %>
    diff --git a/app/views/todos/_todo.html.erb b/app/views/todos/_todo.html.erb index f04b0c8..a6ec100 100644 --- a/app/views/todos/_todo.html.erb +++ b/app/views/todos/_todo.html.erb @@ -9,4 +9,18 @@ <%= todo.description %> +
    + Completed: + <%= todo.completed %> +
    + +
    + Priority: + <%= todo.priority %> +
    + +
    + Project: + <%= link_to todo.project.name, project_path(todo.project) %> +
    diff --git a/db/migrate/20251027123148_add_project_id_to_todos.rb b/db/migrate/20251027123148_add_project_id_to_todos.rb new file mode 100644 index 0000000..cd652c4 --- /dev/null +++ b/db/migrate/20251027123148_add_project_id_to_todos.rb @@ -0,0 +1,5 @@ +class AddProjectIdToTodos < ActiveRecord::Migration[8.1] + def change + add_reference :todos, :project, null: false, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 1dc928a..5dda309 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2025_10_27_122702) 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 @@ -24,6 +24,10 @@ ActiveRecord::Schema[8.1].define(version: 2025_10_27_122702) do 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