Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ def create
[[], 302, { 'location' => location }]
end

def destroy
return require_authentication unless current_user

return category_not_found unless find_category

category_name = @category.name
request.session['flash'] = if @category.destroy
"Category '#{category_name}' deleted"
else
'Failed to delete'
end

[[], 302, { 'location' => '/categories' }]
end

private

def find_category
Expand Down
34 changes: 34 additions & 0 deletions app/views/categories/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,46 @@
<%= category.name %>
</a>
</h5>

<div class="mt-2">
<button type="button"
class="btn btn-sm btn-outline-danger"
data-bs-toggle="modal"
data-bs-target="#deleteModal-<%= category.id %>">
Delete category
</button>

<div class="modal fade" id="deleteModal-<%= category.id %>" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Deletion</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Are you sure you want to delete <strong><%= category.name %></strong>?<br>
This action cannot be undone.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>

<form action="/categories?id=<%= category.id %>" method="POST" style="display: inline;">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn btn-danger">Delete</button>
</form>

</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<% end %>
</div>


<nav class="d-flex justify-content-center mt-3" aria-label="Pages">
<%= pagy_bootstrap_nav(@pagy) if @pagy&.pages.to_i > 1 %>
</nav>
Expand Down
1 change: 1 addition & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use Frack::Router do
get '/categories/new' => 'categories#new'
post '/categories' => 'categories#create'
get '/categories/show' => 'categories#show'
delete '/categories' => 'categories#destroy'
get '/products' => 'products#index'
get '/products/new' => 'products#new'
post '/products' => 'products#create'
Expand Down