title: Redirect/render modal with ajax in rails
body: <p>I have one rails application in which I need to check if the user is not authenticated while using a particular page then it should show login modal on the same page and background should be faded.</p>
  
  <p>For that, I have one method called <code>authenticate user</code> in the application controller</p>
  
  <pre><code>def authenticated_user
   if user
   else
    # from here I want to open a sign in modal
   end
  end
  </code></pre>
  
  <p>My current <code>sessions/new</code> method is following,</p>
  
  <pre><code>class SessionsController &lt; ApplicationController
    def new
     respond_to do |format|
        format.html
        format.js
      end
    end
  end
  </code></pre>
  
  <p>I also have new.js.erb in sessions folder in views,</p>
  
  <pre><code>$("#modal-window").find(".modal-content").html("&lt;%= escape_javascript( render 'sessions/new', formats: [ :html ]) %&gt;");
  $("#modal-window").modal();
  </code></pre>
  
  <p>I have tried sending ajax request with following,</p>
  
  <pre><code>redirect_to sessions_path(format: :js)
  </code></pre>
  
  <p>but it instead redirects me too <code>http://localhost:3000/reader/login.js</code> rather than opening modal on the same page.</p>