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
26 changes: 26 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,34 @@ def create
end
end

def destroy
return require_authentication unless current_user

return product_not_found unless find_product

product_name = @product.name
request.session['flash'] = if @product.destroy
"Product '#{product_name}' deleted"
else
'Delete failed'
end

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


private

def find_product
product_id = request.params['id']
@product = Product.find_by(id: product_id)
end

def product_not_found
request.session['flash'] = 'Product not found'
[[], 302, { 'location' => '/products' }]
end

def product_params
request.params.slice(
'name',
Expand Down
31 changes: 31 additions & 0 deletions app/views/products/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<th scope="col" class="text-center">Year</th>
<th scope="col" class="text-end">Price</th>
<th scope="col" class="text-center">Created At</th>
<th scope="col" class="text-center">Delete</th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -66,6 +67,36 @@
<%= p.created_at.strftime("%m/%d/%Y") %>
</small>
</td>
<td class="text-center">
<div class="mt-2">
<button type="button"
class="btn btn-sm btn-outline-danger"
data-bs-toggle="modal"
data-bs-target="#deleteModal-<%= p.id %>">
Delete product
</button>
</div>
<div class="modal fade" id="deleteModal-<%= p.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><%= p.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="/products?id=<%= p.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>
</td>
</tr>
<% end %>
</tbody>
Expand Down
1 change: 1 addition & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use Frack::Router do
get '/products' => 'products#index'
get '/products/new' => 'products#new'
post '/products' => 'products#create'
delete '/products' => 'products#destroy'
get '/health' => 'jobs#health'
post '/jobs/batch' => 'jobs#batch'
end
Expand Down