Unselect a Seat to remove it from visitor’s Cart
Change the cart summary’s <button>
element to be rendered with a
button_to
helper, transforming its underlying element to
<form>
element with its method
attribute set to
DELETE
.
This commit also introduces the SelectionsController#destroy
action to
handle requests made from the <form>
generated by the button_to
call.
In order to route requests to the selections#destroy
method without
a SeatSelection
record identifier, declare the route as a singular
resource route . To access this action, the page’s
<form>
element must declare its action
attribute to the
/seats/:seat_id/selections
route, and ensure that its method is
DELETE
.
Desktop
Collapse app/controllers/selections_controller.rb
Expand app/controllers/selections_controller.rb
app/controllers/selections_controller.rb
diff --git a/app/controllers/selections_controller.rb b/app/controllers/selections_controller.rb
index 2d6f971..a00c873 100644
--- a/app/controllers/selections_controller.rb
+++ b/app/controllers/selections_controller.rb
@@ -6,4 +6,12 @@ class SelectionsController < ApplicationController
redirect_to venue_floor_seats_url(seat.venue, seat.floor)
end
+
+ def destroy
+ seat = Seat.find(params[:seat_id])
+
+ Current.cart.seat_selections.where(seat_id: seat).destroy_all
+
+ redirect_to venue_floor_seats_url(seat.venue, seat.floor)
+ end
end
Collapse app/views/seats/index.html.erb
Expand app/views/seats/index.html.erb
app/views/seats/index.html.erb
diff --git a/app/views/seats/index.html.erb b/app/views/seats/index.html.erb
index 1fa0c41..8da5260 100644
--- a/app/views/seats/index.html.erb
+++ b/app/views/seats/index.html.erb
@@ -80,14 +80,18 @@
<%= number_to_currency(seat.section.price / 100.0) %>
</td>
<td class="syos-u-text-align-right">
- <button class="syos-button syos-button--transparent">
+ <%= button_to(
+ seat_selection_path(seat),
+ class: "syos-button syos-button--transparent",
+ method: :delete,
+ ) do %>
<%= inline_svg_tag(
"icons/x-circle.svg",
aria: true,
class: "syos-icon",
title: "Remove",
) %>
- </button>
+ <% end %>
</td>
</tr>
<% end %>
Collapse config/routes.rb
Expand config/routes.rb
config/routes.rb
diff --git a/config/routes.rb b/config/routes.rb
index 1f8d5ed..8ff545e 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -8,6 +8,7 @@ Rails.application.routes.draw do
resources :seats, only: [] do
resources :selections, only: [:create]
+ resource :selection, only: [:destroy]
end
root to: redirect("/venues/benedum_center/floors/orchestra/seats")
Collapse test/controllers/selections_controller_test.rb
Expand test/controllers/selections_controller_test.rb
test/controllers/selections_controller_test.rb
diff --git a/test/controllers/selections_controller_test.rb b/test/controllers/selections_controller_test.rb
index f4a817d..76b31c9 100644
--- a/test/controllers/selections_controller_test.rb
+++ b/test/controllers/selections_controller_test.rb
@@ -19,4 +19,15 @@ class SelectionsControllerTest < ActionDispatch::IntegrationTest
assert_equal cart.seats.ids, [seat.id]
end
+
+ test "#destroy when a Seat is already selected" do
+ seat_selection = create(:seat_selection)
+ cart = seat_selection.cart
+ seat = seat_selection.seat
+ cookies[:cart_token] = cart.token
+
+ delete seat_selection_path(seat, seat_selection)
+
+ assert_equal cart.seats.ids, []
+ end
end
Collapse test/system/visitor_unselects_seat_test.rb
Expand test/system/visitor_unselects_seat_test.rb
test/system/visitor_unselects_seat_test.rb
diff --git a/test/system/visitor_unselects_seat_test.rb b/test/system/visitor_unselects_seat_test.rb
new file mode 100644
index 0000000..bfb6a23
--- /dev/null
+++ b/test/system/visitor_unselects_seat_test.rb
@@ -0,0 +1,18 @@
+require "application_system_test_case"
+
+class VisitorUnselectsSeatTest < ApplicationSystemTestCase
+ test "visiting the seat page" do
+ venue = create(:benedum_center)
+ floor = create(:orchestra, venue: venue)
+ section = create(:section, floor: floor, price: 10_00)
+ seat = create(:seat, row: "AA", number: "101", section: section)
+
+ visit "/venues/benedum_center/floors/orchestra/seats/AA-101"
+ click_on "Select"
+ click_on "Remove"
+
+ within "#cart-summary" do
+ assert_no_text "$10.00"
+ end
+ end
+end