我想在登錄後將用戶重定向到同一頁面。這種情況是,如果用戶未登錄,用戶將被重定向到登錄頁面。在登錄頁面上,當他點擊其中一個可用用戶選項時,我想存儲user_id,然後將其重定向到相同的用戶選項他來自哪個頁面。 對於這一點,我用下面的代碼:登錄後將用戶重定向到同一頁面(Rails)
在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
在SigninController:
class SigninController < ApplicationController
def index
session[:user] = params[:user_id]
redirect_to session[:referer]
end
end
在登入/索引頁:
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 %>
我得到的錯誤:
user_id沒有被保存,而重定向時,我得到一個錯誤,說session [:referer]是nil。
請解釋一下我做錯了
當你這樣做:'如果request.format.html? ''在'''''''''''''''''''''''''''那就是爲什麼'會議[:referer]'是零。 – 7urkm3n
你在哪裏保存'referrer'到會話? –
在會話[:referer]中。如果用戶未登錄,它將被保存。請查看application_helper.rb @Hieu Pham –