Repository: rails/rails
File: actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (Ruby)

Code before:
<% names = traces.keys %>
<% error_index = local_assigns[:error_index] || 0 %>

<p><code>Rails.root: <%= defined?(Rails) && Rails.respond_to?(:root) ? Rails.root : "unset" %></code></p>

<div id="traces-<%= error_index %>">
  <% names.each do |name| %>
    <%
      show = "show('#{name.gsub(/\s/, '-')}-#{error_index}');"
      hide = (names - [name]).collect {|hide_name| "hide('#{hide_name.gsub(/\s/, '-')}-#{error_index}');"}
    %>
    <a href="#" onclick="<%= hide.join %><%= show %>; return false;"><%= name %></a> <%= '|' unless names.last == name %>
  <% end %>

  <% traces.each do |name, trace| %>
    <div id="<%= "#{name.gsub(/\s/, '-')}-#{error_index}" %>" class="trace-container" style="display: <%= (name == trace_to_show) ? 'block' : 'none' %>;">
      <code class="traces">
        <% trace.each do |frame| %>
          <div class="trace">
            <% file_url = ActionDispatch::TraceToFileExtractor.open_in_editor? && ActionDispatch::TraceToFileExtractor.new(frame[:trace]).call %>
            <% if file_url.present? %>
              <a href="<%= file_url %>" class="edit-icon">
                <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
                  <path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
                </svg>
              </a>
            <% end %>
            <a class="trace-frames trace-frames-<%= error_index %>" data-exception-object-id="<%= frame[:exception_object_id] %>" data-frame-id="<%= frame[:id] %>" href="#">
              <%= frame[:trace] %>
            </a>
            <br>
          </div>
        <% end %>
      </code>
    </div>
  <% end %>

  <script>
    (function() {
      var traceFrames = document.getElementsByClassName('trace-frames-<%= error_index %>');
      var selectedFrame, currentSource = document.getElementById('frame-source-<%= error_index %>-0');

      // Add click listeners for all stack frames
      for (var i = 0; i < traceFrames.length; i++) {
        traceFrames[i].addEventListener('click', function(e) {
          e.preventDefault();

Review comment (refactor):
Minor suggestion: We could use [link_to_if](https://api.rubyonrails.org/v8.0.1/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_if)

Code after:
<% names = traces.keys %>
<% error_index = local_assigns[:error_index] || 0 %>

<p><code>Rails.root: <%= defined?(Rails) && Rails.respond_to?(:root) ? Rails.root : "unset" %></code></p>

<div id="traces-<%= error_index %>">
  <% names.each do |name| %>
    <%
      show = "show('#{name.gsub(/\s/, '-')}-#{error_index}');"
      hide = (names - [name]).collect {|hide_name| "hide('#{hide_name.gsub(/\s/, '-')}-#{error_index}');"}
    %>
    <a href="#" onclick="<%= hide.join %><%= show %>; return false;"><%= name %></a> <%= '|' unless names.last == name %>
  <% end %>

  <% traces.each do |name, trace| %>
    <div id="<%= "#{name.gsub(/\s/, '-')}-#{error_index}" %>" class="trace-container" style="display: <%= (name == trace_to_show) ? 'block' : 'none' %>;">
      <code class="traces">
        <% trace.each do |frame| %>
          <div class="trace">
            <% file_url = editor_url(frame[:trace]) %>
            <%= file_url && link_to("✏️", file_url, class: "edit-icon") %>
            <a class="trace-frames trace-frames-<%= error_index %>" data-exception-object-id="<%= frame[:exception_object_id] %>" data-frame-id="<%= frame[:id] %>" href="#">
              <%= frame[:trace] %>
            </a>
            <br>
          </div>
        <% end %>
      </code>
    </div>
  <% end %>

  <script>
    (function() {
      var traceFrames = document.getElementsByClassName('trace-frames-<%= error_index %>');
      var selectedFrame, currentSource = document.getElementById('frame-source-<%= error_index %>-0');

      // Add click listeners for all stack frames
      for (var i = 0; i < traceFrames.length; i++) {
        traceFrames[i].addEventListener('click', function(e) {
          e.preventDefault();
          var target = e.target;
          var frame_id = target.dataset.frameId;

          if (selectedFrame) {
            selectedFrame.className = selectedFrame.className.replace("selected", "");
          }