我試圖將用戶重定向到他們在登錄之前嘗試訪問的頁面(該頁面只對用戶可見)。Rails重定向到頁面用戶想要在登錄之前查看
我會與前過濾器,針對我的旅行新的行動開始:
before_filter :authenticate, :only => [:new, :create, :edit, :update, :destroy]
def authenticate
deny_access unless signed_in?
end
def deny_access
store_location
redirect_to login_path, :notice => "Please log in to access this page."
end
def store_location
session[:return_to] = request.fullpath
end
這是新的,在我sessions_controller.rb創建操作
def new
@title = "Log in"
end
def create
user = User.authenticate(params[:session][:email].downcase,
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Log in"
render 'new'
else
sign_in user
redirect_back_or(root_path)
end
end
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
clear_return_to
end
謝謝!
編輯:刪除MaddHacker的我的代碼版本,因爲這不是我想要的。
解決的問題。在我重定向到登錄頁面之前,我只需在控制器中添加'store_location'。 – Huy