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:
我想在登录后将用户重定向到相同的页面。这种情况是,如果用户没有登录,将被重定向到登录页面。在登录页面上,当他单击一个可用的用户选项时,我希望存储user_id,然后将他重定向回他来自的页面。为此,我使用以下代码:
In application_helper.rb:
引入application_helper . rb之后在:
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
In SigninController:
在SigninController:
class SigninController < ApplicationController
def index
session[:user] = params[:user_id]
redirect_to session[:referer]
end
end
In signin/index page:
在signin /索引页:
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.
<%= link_to 'Sam', controller:"signin", action: "index" , user_id: 284542812, remote: true %> <%= link_to 'Marge', controller:"signin", action: "index" , user_id: 604700687, remote: true %>
<%= link_to 'Sam', controller:"signin", action: "index", user_id: 284542812, remote: true %> <%= link_to 'Marge', controller:"signin", action: "index", user_id: 604700687, remote: true %>。
Error that I'm getting:
我得到错误:
the user_id is not being saved and while redirecting I get an error saying that session[:referer] is nil.
user_id没有被保存,在重定向时,我得到一个错误,说session[:referer]是nil。
Please explain what am I doing wrong
请解释一下我做错了什么
1 个解决方案
#1
2
You are calling it async if request.format.html?
then asking a format is html?
it returns false
. Since it returns false
you are not able to store this session session[:referer] = request.url
.
如果是request.format.html?那么问格式是html吗?它返回false。由于它返回false,所以您无法存储此会话[:referer] = request.url。
Also Rails has redirect_to :back
option too. Try this below first, and let me know...
Rails也有redirect_to:back选项。先试试下面的方法,让我知道……
Helper:
助手:
module ApplicationHelper
def check_signed_in
if session[:user]
session[:referer] = request.url
end
end
end
Controller:
控制器:
class SigninController < ApplicationController
include ApplicationHelper
def index
check_signed_in
session[:user] = params[:user_id]
redirect_to session[:referer]
end
end
#1
2
You are calling it async if request.format.html?
then asking a format is html?
it returns false
. Since it returns false
you are not able to store this session session[:referer] = request.url
.
如果是request.format.html?那么问格式是html吗?它返回false。由于它返回false,所以您无法存储此会话[:referer] = request.url。
Also Rails has redirect_to :back
option too. Try this below first, and let me know...
Rails也有redirect_to:back选项。先试试下面的方法,让我知道……
Helper:
助手:
module ApplicationHelper
def check_signed_in
if session[:user]
session[:referer] = request.url
end
end
end
Controller:
控制器:
class SigninController < ApplicationController
include ApplicationHelper
def index
check_signed_in
session[:user] = params[:user_id]
redirect_to session[:referer]
end
end