From 11df9c3589a87079af98c7b13497727bd76da84e Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Wed, 18 Mar 2026 22:03:36 -0400 Subject: [PATCH] Fix back navigation from activity page to cards (#2727) Three changes needed to support navigating back from a card to the activity page: - Add root_path to the prefer_referrer allowlist on the card show page - Switch event links from HTML target="_top" to data-turbo-frame="_top" so Turbo handles the navigation and turbo:before-visit fires to save the referrer - Normalize trailing slashes in the referrer path comparison so /account_id and /account_id/ both match ref: https://app.fizzy.do/5986089/cards/2390 --- .../turbo_navigation_controller.js | 5 ++-- app/views/cards/show.html.erb | 2 +- app/views/events/event/_layout.html.erb | 2 +- test/system/back_link_navigation_test.rb | 24 +++++++++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/javascript/controllers/turbo_navigation_controller.js b/app/javascript/controllers/turbo_navigation_controller.js index 8f47dac9b..169fb93be 100644 --- a/app/javascript/controllers/turbo_navigation_controller.js +++ b/app/javascript/controllers/turbo_navigation_controller.js @@ -24,8 +24,9 @@ export default class extends Controller { referrerBackLinkTargetConnected(link) { if (!this.#referrerUrl || !this.#referrerLabel) { return } - const allowedPaths = (link.dataset.turboNavigationAllowedReferrerPaths || "").split(",") - const referrerPath = new URL(this.#referrerUrl).pathname + const stripTrailingSlash = path => path.replace(/\/$/, "") + const allowedPaths = (link.dataset.turboNavigationAllowedReferrerPaths || "").split(",").map(stripTrailingSlash) + const referrerPath = stripTrailingSlash(new URL(this.#referrerUrl).pathname) if (!allowedPaths.includes(referrerPath)) { return } link.href = this.#referrerUrl diff --git a/app/views/cards/show.html.erb b/app/views/cards/show.html.erb index fd9101d58..9acb305bf 100644 --- a/app/views/cards/show.html.erb +++ b/app/views/cards/show.html.erb @@ -7,7 +7,7 @@ <% content_for :header do %>
- <%= link_back_to_board @card.board, prefer_referrer: [ cards_path, board_path(@card.board) ] %> + <%= link_back_to_board @card.board, prefer_referrer: [ root_path, cards_path, board_path(@card.board) ] %>
<% end %> diff --git a/app/views/events/event/_layout.html.erb b/app/views/events/event/_layout.html.erb index 4a78490e8..8b7d2d520 100644 --- a/app/views/events/event/_layout.html.erb +++ b/app/views/events/event/_layout.html.erb @@ -2,8 +2,8 @@ id: dom_id(event, "timelined"), class: "event event--#{ event.action } #{ "golden-effect" if event.card.golden? } center-block flex flex-column full-width align-start justify-start position-relative", style: "--card-color: #{ card.closed? ? "var(--color-card-complete)" : card.color };", - target: "_top", data: { + turbo_frame: "_top", related_element_target: "related", related_element_group_value: card.id, action: "mouseover->related-element#highlight mouseout->related-element#unhighlight" } do %> diff --git a/test/system/back_link_navigation_test.rb b/test/system/back_link_navigation_test.rb index e46bff521..981115bf2 100644 --- a/test/system/back_link_navigation_test.rb +++ b/test/system/back_link_navigation_test.rb @@ -26,6 +26,30 @@ class BackLinkNavigationTest < ApplicationSystemTestCase assert_current_path filter_url, ignore_query: false end + test "card back link returns to activity page when navigating from it" do + sign_in_as(users(:david)) + + assert_text "Layout is broken" + click_on "Layout is broken" + + assert_selector "a.btn--back strong", text: "Back to Home" + find("a.btn--back").click + assert_current_path root_path + end + + test "card back link returns to activity page when navigating from it without trailing slash" do + sign_in_as(users(:david)) + + activity_url_without_trailing_slash = root_url.chomp("/") + visit activity_url_without_trailing_slash + assert_text "Layout is broken" + click_on "Layout is broken" + + assert_selector "a.btn--back strong", text: "Back to Home" + find("a.btn--back").click + assert_current_path activity_url_without_trailing_slash + end + test "card back link is not rewritten when navigating from a non-filter page" do sign_in_as(users(:david))