Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
Basic Delete Functionality
Browse files Browse the repository at this point in the history
You can delete a task on the index page, with a basic confirm dialog box
to prevent accidental deletion.
  • Loading branch information
awood45 committed Jun 23, 2014
1 parent 5ede6bf commit a42dbd7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
10 changes: 10 additions & 0 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ def update
end
end

def destroy
@task = Task.find(params[:id])
if @task.delete
redirect_to tasks_path
else
flash[:error] = "Task deletion failed! #{print_errors(@task)}"
redirect_to tasks_path
end
end

def task_create_params
params.permit(:name)
end
Expand Down
11 changes: 7 additions & 4 deletions app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
task_path(id: task.id, completed_flag: false),
method: "patch",
class: "btn btn-sm btn-danger" ) %>
<%= task.name %>
</li>
<% else %>
<li class='list-group-item task-item' id="task_<%= task.id %>">
<%= link_to('Mark Complete',
task_path(id: task.id, completed_flag: true),
method: 'patch',
:class => 'btn btn-sm btn-success') %>
<%= task.name %>
</li>
<% end %>
<%= link_to("Delete",
task_path(id: task.id),
method: "delete",
class: "btn btn-sm btn-warning",
data: { confirm: "Are you sure?" }) %>
<%= task.name %>
</li>
<% end %>
<li class='list-group-item' id="new-task-form">
<%= form_tag("/tasks", method: "post") do %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
resources :tasks, only: [:index, :create, :update]
resources :tasks, only: [:index, :create, :update, :destroy]

# Example resource route with options:
# resources :products do
Expand Down

0 comments on commit a42dbd7

Please sign in to comment.