Redirecting the user to the same page after login (Rails)
<p>I want to redirect the user to the same page after login. The scenario is that the user will be redirected to a signin page if he is not logged in. On the signin page, when he clicks on one of the available user options, I want to store the user_id and then redirect him back to the same page where he came from.
For this, I'm using the following code:</p>

<p>In application_helper.rb:</p>

<pre><code>module ApplicationHelper
def check_signed_in
if session[:user] == nil
  if request.format.html?
    session[:referer] = request.url
    redirect_to signin_index_path
  else
    @error = 'Please Signin!'
    @forward_page = '/signin'
    render 'signin/show_signin.js'
  end
end
end
end
</code></pre>

<p>In SigninController:</p>

<pre><code>class SigninController &lt; ApplicationController
def index
session[:user] = params[:user_id]
redirect_to session[:referer]
end 
end
</code></pre>

<p>In signin/index page:</p>

<pre><code>We simulate a signin here. Click on an user and you will logged in as the selected user and you will be redirected the previous page.
</code></pre>

<p><code>&lt;%= link_to  'Sam', controller:"signin", action: "index" , user_id: 284542812, remote: true %&gt;
&lt;%= link_to  'Marge', controller:"signin", action: "index" , user_id: 604700687, remote: true %&gt;</code></p>

<p>Error that I'm getting:</p>

<p>the user_id is not being saved and while redirecting I get an error saying that session[:referer] is nil.</p>

<p>Please explain what am I doing wrong</p>