Skip to content

Commit 3463eb4

Browse files
edit a task complete with tests passing
1 parent af47be8 commit 3463eb4

9 files changed

Lines changed: 6347 additions & 42 deletions

File tree

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,81 @@
1-
# class Task
2-
# def initialize(name)
3-
# @name = name
4-
# @complete = false
5-
# end
6-
# end
7-
8-
TASKS = [
9-
{name: 'Buy Jesse a toy', complete: false},
10-
{name: 'Percy needs flea pills', complete: true},
11-
{name: 'Reload chantals coffee card', complete: true}
12-
]
13-
141
class TasksController < ApplicationController
152
def root
163
@message = 'Hello world!'
174
end
185

196
def show
20-
@task = Task.find(params['id'])
7+
# This is the ruby try-catch.
8+
# If Task.find throws an exception
9+
# (e.g., can't find a task with that id)
10+
# then we can catch the exception and
11+
# redirect the user to the homepage with a message.
12+
begin
13+
@task = Task.find(params['id'])
14+
rescue
15+
flash[:error] = "Could not find task with id: #{params['id']}" #not printing??? why!
16+
redirect_to tasks_path
17+
return
18+
end
2119
end
2220

2321
def index
2422
@tasks = Task.all
25-
#@tasks = TASKS
2623
end
24+
25+
def create
26+
unless params.key?(:task)
27+
@task = Task.new
28+
else
29+
@task = Task.new(
30+
name: params[:task][:name],
31+
description: params[:task][:description]
32+
)
33+
if @task.save
34+
new_task = Task.find_by(name: @task.name)
35+
redirect_to task_path(new_task.id)
36+
else
37+
render :new
38+
end
39+
end
40+
end
41+
42+
def edit
43+
begin
44+
@task = Task.find(params['id'])
45+
rescue
46+
flash[:error] = "Could not find task with id: #{params['id']}"
47+
redirect_to tasks_path
48+
return
49+
end
50+
end
51+
52+
def update
53+
begin
54+
@task = Task.find(params[:id])
55+
rescue
56+
flash[:error] = "Could not find task with id: #{params['id']}"
57+
redirect_to tasks_path
58+
return
59+
end
60+
61+
begin
62+
if params[:task].key?(:completion_date)
63+
@task.update(
64+
name: params[:task][:name],
65+
description: params[:task][:description],
66+
completion_date: params[:task][:completion_date]
67+
)
68+
else
69+
@task.update(
70+
name: params[:task][:name],
71+
description: params[:task][:description]
72+
)
73+
end
74+
redirect_to task_path(params[:id])
75+
rescue
76+
render :new
77+
return
78+
end
79+
end
80+
2781
end

app/views/tasks/create.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1>New Task</h1>
2+
<%= link_to 'Back to tasks', tasks_path %>
3+
<%= form_with model: @task do |f| %>
4+
<ul>
5+
<li>Name: <%= f.text_field :name %></li>
6+
<li>Description: <%= f.text_field :description %></li>
7+
</ul>
8+
<%= f.submit "Save task" %>
9+
<% end %>

app/views/tasks/edit.html.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<h1>Form for editing an existing Task</h1>
2+
<%= link_to 'Back to all tasks', tasks_path %>
3+
<%= form_with model: @task, class: 'edit-book' do |f| %>
4+
<p> Please fill out this form to edit a task </p>
5+
<ul>
6+
<li>Name: <%= f.text_field :name %></li>
7+
<li>Description: <%= f.text_field :description %></li>
8+
<%= f.submit "Save Task", class: "task-form_submit-btn"%>
9+
</ul>
10+
<% end %>

app/views/tasks/index.html.erb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<h1>Tasks</h1>
2+
<%= link_to 'Create task', create_task_path %>
23
<ul>
34
<% @tasks.each do |task| %>
45
<li>
5-
<%= link_to task.name, task_path(task.id) %>
66
<% if task.completion_date %>
7-
<s><%= task.name %></s>
7+
<s><%= link_to task.name, task_path(task.id) %></s>
88
<% else %>
9-
<%= task.name %>
9+
<%= link_to task.name, task_path(task.id) %>
1010
<% end %>
1111
</li>
1212
<% end %>
13+
</ul>

app/views/tasks/show.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
<li>completed on <%= @task.completion_date %></li>
1717
<% end %>
1818
</ul>
19-
19+
<%= link_to "Edit", edit_task_path %>
20+
<br />
2021
<%= link_to "Back to all Tasks", tasks_path %>

config/routes.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
33
root :to => 'tasks#root' #re-read ada lesson this looks weird
44
get '/tasks', to: 'tasks#index', as: 'tasks'
5+
get '/tasks/create', to: 'tasks#create', as: 'create_task'
56
get '/tasks/:id', to: 'tasks#show', as: 'task'
6-
7+
post '/tasks', to: 'tasks#create'
8+
get '/tasks/:id/edit', to: 'tasks#edit', as: 'edit_task'
9+
patch '/tasks/:id', to: 'tasks#update', as: 'update_task'
710
end

0 commit comments

Comments
 (0)