gpt4 book ai didi

ruby-on-rails - 设计 - 如果用户未登录,则重定向到登录后点击的用户

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:34 25 4
gpt4 key购买 nike

我在 Rails 3 中使用 Devise。我使用 before_filter :authenticate_user! 在用户未登录的情况下单击链接时重定向以登录。这有效,但登录后,它会重定向回主页。相反,我想重定向到用户在重定向到登录页面之前单击的链接。这是我的代码:

application_controller.rb

class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate_user!
layout :layout_by_resource

def after_sign_in_path_for(resource_or_scope)
if resource_or_scope.is_a?(User)
if current_user.admin?
admin_event_types_path
else
home_path
end
else
super
end
end
..
..
end

用户.rb

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:token_authenticatable
..
..
end

更新

我尝试使用@dreamfall 的回答进行此操作,但它没有按预期进行重定向。

def after_sign_in_path_for(resource_or_scope)
if resource_or_scope.is_a?(User)
if current_user.admin?
stored_location_for(resource_or_scope) ? stored_location_for(resource_or_scope) : admin_event_types_path
else
stored_location_for(resource_or_scope) ? stored_location_for(resource_or_scope) : home_path
end
else
super
end
end

在 Devise 中是否有针对此的内置配置?如果没有,我将如何解决这个问题,我应该在哪里更改代码?

最佳答案

Devise 提供了在登录后重定向到存储在 session url 中的功能。在 after_sign_in_path_for(resource_or_scope) 方法中使用 stored_location_for(resource_or_scope) 方法。

关于ruby-on-rails - 设计 - 如果用户未登录,则重定向到登录后点击的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9743185/

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