title: How to show the modal with alarm when opening the link in the email (Rails6+Bootstrap4)
body: <h3>What I want to solve</h3>
  <p>I've been making original web app using <a href="https://www.railstutorial.org/book" rel="nofollow noreferrer">the ruby on rails tutorials</a> as a guide,
  now I'm stuck on the password resetting part in chapter 12.
  If the password reset link which mentioned email has been expired with password_expiration method in before_action, it should visit to root view and display modal for password reset with alert &quot;link expired&quot;, but it can't be got to work.
  testing it with rspec. I would be something wrong, but I don't know how to fix it. I would be graceful for your advice.</p>
  <h3>Current situations</h3>
  <p>This is a part of relevant <code>password_resets_controller</code></p>
  <pre><code>class PasswordResetsController &lt; ApplicationController
  
    before_action :check_expiration, only: [:edit, :update]
  
    protect_from_forgery except: :new
  
    def new
  
    end
  
    private
  
      def check_expiration
        if @user.password_reset_expired?
          respond_to do |format|
            format.html { 
              flash[:danger] = 'link expired'
              redirect_to root_url 
            }
            format.js {
              redirect_to root_url
              render :new
              render :expired
            }
          end
        end
      end
  end
  </code></pre>
  <p>The contents of <code>password_reset_expired?</code></p>
  <pre><code>class User &lt; ApplicationRecord
  
  def password_reset_expired?
      reset_sent_at &lt; 2.hours.ago
  end
  </code></pre>
  <p>The content of the email template.
  I set the link to remote: true here, so I thought it would work with format.js, but it doesn't?</p>
  <pre><code>
  &lt;p&gt;please follow the link below to reset your password&lt;/p&gt;
  
  &lt;%= link_to &quot;password reset&quot;, edit_password_reset_url(@user.reset_token, 
                                   email: @user.email), remote: true %&gt;
  
  </code></pre>
  <p>Contents of <code>new.js.erb</code>, <code>expired.js.erb</code> and <code>_expired_comment.html.erb</code><br/>
  <code>new.js.erb</code> calls modal.</p>
  <pre><code>$(&quot;#password-set-modal&quot;).html(&quot;&lt;%= escape_javascript(render 'form-modal') %&gt;&quot;)
  $(&quot;#password-set-modal&quot;).modal(&quot;show&quot;)
  $(&quot;#login-modal&quot;).modal(&quot;hide&quot;)
  </code></pre>
  <pre><code>$(&quot;.js-message-error&quot;).html(&quot;&lt;%= escape_javascript(render 'password_resets/expired_comment') %&gt;&quot;);
  </code></pre>
  <pre><code>&lt;p style=&quot;color: #bb4a4a;&quot;&gt;link expired&lt;/p&gt;
  </code></pre>
  <p>Testing rspec</p>
  <pre><code>require 'rails_helper'
  
  RSpec.describe &quot;PasswordResets&quot;, type: :system do
    let(:user) { create :user }
    path_regex = /(?:https?\:\/\/.*?)(?:localhost\:3000)(?:\/password\_resets)(\/.*?)(?:\/edit\?email=.*\..{2,3})/
    
    before do
      ActionMailer::Base.deliveries.clear
    end
  
    describe 'reset password' do
      before do
        visit root_path
        find('.navbar-toggler').click
        page.evaluate_script('$(&quot;.fade&quot;).removeClass(&quot;fade&quot;)')
        click_on 'login'
        click_on '(forget?)'
      end
  
      context 'when access the link which has been expired' do
        it 'open modal with alert' do
          fill_in 'email', visible: true, with: user.email
          click_on 'send', visible: true
          sleep 1
    
          part =
            ActionMailer::Base
              .deliveries
              .last
              .body
              .parts
              .detect { |p| p.content_type == &quot;text/html; charset=UTF-8&quot; }
          password_reset_mail = part.body.raw_source
          password_reset_url = password_reset_mail.match(path_regex)[0]
  
          user.update_attribute(:reset_sent_at, 3.hours.ago)
  
          visit URI(password_reset_url).request_uri
  
          sleep 1
  
          expect(page).to have_selector('.modal', visible: true)
          expect(page).to have_content('link expired')
          
        end
      end
    end
  end
  </code></pre>