Skip to content

Commit cfaaa84

Browse files
delete a task and mark a task complete
1 parent 3463eb4 commit cfaaa84

4 files changed

Lines changed: 717 additions & 0 deletions

File tree

app/controllers/tasks_controller.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,39 @@ def update
7878
end
7979
end
8080

81+
def complete
82+
begin
83+
@task = Task.find(params[:id])
84+
rescue
85+
flash[:error] = "Could not find task with id: #{params['id']}"
86+
redirect_to tasks_path
87+
return
88+
end
89+
90+
if @task.completion_date.nil?
91+
@task.update(
92+
completion_date: Date.today
93+
)
94+
else
95+
@task.update(
96+
completion_date: nil
97+
)
98+
end
99+
100+
redirect_to tasks_path
101+
end
102+
103+
def delete
104+
begin
105+
@task = Task.find(params[:id])
106+
rescue
107+
# make this into a method if i have time. to dry it up
108+
flash[:error] = "Could not find task with id: #{params['id']}"
109+
redirect_to tasks_path
110+
return
111+
end
112+
113+
@task.destroy
114+
redirect_to tasks_path
115+
end
81116
end

app/views/tasks/index.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
<% @tasks.each do |task| %>
55
<li>
66
<% if task.completion_date %>
7+
<%= link_to "☑", complete_task_path(task.id), method: :patch %>
78
<s><%= link_to task.name, task_path(task.id) %></s>
89
<% else %>
10+
<%= link_to "☐", complete_task_path(task.id), method: :patch %>
911
<%= link_to task.name, task_path(task.id) %>
1012
<% end %>
13+
<%= link_to 'X', delete_task_path(task.id), method: :delete, data: {comfirm: 'Are you sure you want to delete?'} %>
1114
</li>
1215
<% end %>
1316
</ul>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
post '/tasks', to: 'tasks#create'
88
get '/tasks/:id/edit', to: 'tasks#edit', as: 'edit_task'
99
patch '/tasks/:id', to: 'tasks#update', as: 'update_task'
10+
delete '/tasks/:id/delete', to: 'tasks#delete', as: 'delete_task'
11+
patch '/tasks/:id/complete', to: 'tasks#complete', as: 'complete_task'
1012
end

0 commit comments

Comments
 (0)