gpt4 book ai didi

ruby-on-rails - 登录后重定向到上一个 URL - Rails

转载 作者:行者123 更新时间:2023-12-04 20:51:02 26 4
gpt4 key购买 nike

对于我正在编写的应用程序,我使用的是简单的手工验证(如 Railscast.com 上所述)。使用 Ryan Bates 的 NiftyGenerators gem 中的一些代码,我有一个身份验证模型,其中包含一些有用的身份验证方法。该模块包含在 application_controller.rb 中.

我想使用的方法之一是 redirect_to_target_or_default .我知道这是我需要将用户重定向到他们通过身份验证后所在的页面,但我不知道我应该在哪里调用此方法?如果有人能给我一个关于如何使用这种方法的想法,我将不胜感激。

Controller 认证模块代码

module ControllerAuthentication
# Makes these methods helper methods in the controller that includes this module.
def self.included(controller)
controller.send :helper_method,
:current_admin, :logged_in?,
:redirect_to_target_or_default
end

def current_admin
@current_admin ||= Admin.find(session[:admin_id]) if session[:admin_id]
end

def logged_in?
current_admin
end

def login_required
unless logged_in?
store_target_location
redirect_to login_url,
:alert => "You must first log in before accessing this page."
end
end
def redirect_to_target_or_default(default, *args)
redirect_to(session[:return_to] || default, *args)
session[:return_to] = nil
end
def redirect_to_or_default(target, *args)
redirect_to(target || default, *args)
end
def store_target_location
session[:return_to] = request.url
end
end

最佳答案

您是否在 Ryan 的 gem 中运行了生成器?它应该已经为您生成了一个 SessionController(请参阅 link ),其中还包含以下方法:

def create
@session = Session.new(params[:session])
if @session.save
flash[:notice] = "Logged in successfully."
redirect_to_target_or_default(root_url)
else
render :action => 'new'
end
end

我认为您可以在阅读此代码时了解如何使用它。 :)

关于ruby-on-rails - 登录后重定向到上一个 URL - Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10697373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com