Skip to content

Commit 40a5903

Browse files
Christopher PowerChristopher Power
authored andcommitted
Connect Projects and Todos
1 parent a93054a commit 40a5903

File tree

8 files changed

+34
-2
lines changed

8 files changed

+34
-2
lines changed

app/controllers/todos_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ def set_todo
6565

6666
# Only allow a list of trusted parameters through.
6767
def todo_params
68-
params.expect(todo: [ :name, :description ])
68+
params.expect(todo: [ :name, :description, :completed, :priority, :project_id ])
6969
end
7070
end

app/models/project.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
class Project < ApplicationRecord
2+
has_many :todos, dependent: :destroy
23
validates :name, presence: { message: "Was forgotten?" }
34
end

app/models/todo.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class Todo < ApplicationRecord
2+
belongs_to :project
23
end

app/views/projects/show.html.erb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
<h1><%= @project.name %></h1>
22
<p>Project details coming soon...</p>
33

4+
<h2>Todos for this project</h2>
5+
6+
<ul>
7+
<% @project.todos.each do |todo| %>
8+
<li>
9+
<%= link_to todo.name, todo_path(todo) %>
10+
<% if todo.completed %><% end %>
11+
</li>
12+
<% end %>
13+
</ul>
14+
415
<%= button_to "Delete Project", project_path(@project), method: :delete %>

app/views/todos/_form.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
<%= form.textarea :description %>
2222
</div>
2323

24+
<%= form.label :completed %>
25+
<%= form.check_box :completed %>
26+
27+
<%= form.label :priority %>
28+
<%= form.number_field :priority %>
29+
30+
<%= form.label :project_id %>
31+
<%= form.collection_select :project_id, Project.all, :id, :name, prompt: "Select a project" %>
32+
2433
<div>
2534
<%= form.submit %>
2635
</div>

app/views/todos/_todo.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
<%= todo.description %>
1010
</p>
1111

12+
<%= link_to todo.project.name, project_path(todo.project) if todo.project %>
1213
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddProjectIdToTodos < ActiveRecord::Migration[8.0]
2+
def change
3+
add_reference :todos, :project, null: false, foreign_key: true
4+
end
5+
end

db/schema.rb

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)