Eli*_*ray 3 ruby-on-rails devise
我在stored_location_for这里阅读了几篇有关故障排除的帖子,但似乎无法弄明白并且不确定如何排除故障.
我尝试删除我的自定义after_sign_in_path_for,但这也无效.我的位置永远不会被保存,虽然我理解它在每次会话/页面更新后它应该存储位置.我是否需要手动将其作为过滤器?
def after_sign_in_path_for(resource)
stored_location_for(resource) ||
if resource.is_a?(Account)
add_quote_to_account(resource)
if resource.applications.any?
edit_application_path(resource.applications(true).last)
else
root_path
end
else
super
end
end
Run Code Online (Sandbox Code Playgroud)
可能是您在使用devise登录后没有存储您想要redirect_to的位置.Devise提供了两种方法 - store_location_for和stored_location_for https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/store_location.rb
假设您的用户模型名为"user"
在控制器中调用store_location_for(:user,my_desired_path)会将URL"my_desired_path"存储在会话中,密钥为"user_return_to".基本上这个方法只会这样做 - session ["user_return_to"] = my_desired_path.可能你错过了这个.我有一个预订控制器和一个"登录"操作,用于存储预订控制台中的结账位置,然后在呈现的视图中显示用户的登录表单 -
def login
my_desired_path = url_for(controller: 'bookings', action: 'checkout')
store_location_for(:user, my_desired_path)
end
Run Code Online (Sandbox Code Playgroud)现在,您可以使用stored_location_for(:user)从会话中检索my_desired_path.因此,对stored_location_for(:user)的调用将返回"my_desired_path".
现在,如果您在自定义的after_sign_in_path_for(:user)中使用stored_location_for,那么它将返回"my_desired_path".
附加点 -
对stored_location_for(:user)的调用返回会话[:user_return_to],但如果您的重定向格式是导航格式,则在返回值后清除此会话.因此,对stored_location_for(:user)的第二次调用将不会返回my_desired_path.有时这就是我们希望应用程序运行的方式.相反,如果您的重定向格式不是导航格式,则会话不会被清除,第二次登录将再次重定向到相同的"my_desired_path".
有时,您希望从应用程序中的不同页面重定向到登录的不同位置.假设您要重定向到第A页的"\ X"和第B页的"\ Y".现在按照以下步骤操作 -
因此,请在您的应用程序中处理它.在使用AJAX进行登录的应用程序中更有可能发生这种情况.
| 归档时间: |
|
| 查看次数: |
3130 次 |
| 最近记录: |