[英]Rails - Devise - Redirect to current page after login or logout
I am working on a Ruby on Rails application and using Devise for user authentication. 我正在研究Ruby on Rails应用程序,并使用Devise进行用户身份验证。 However, whenever I login or logout (from any page) I am always redirected to the home page "/".
但是,无论何时(从任何页面)登录或注销,我总是会重定向到主页“ /”。 While what I'm trying to do is that when I login from any page (eg "/events") I am redirected back to this exact page ("/events" in this case) after login.
虽然我想做的是,当我从任何页面登录时(例如“ / events”),登录后我都被重定向回该页面(在本例中为“ / events”)。 And the same goes for logout.
对于注销也是如此。
I tried following this link . 我尝试了此链接 。 But the output of
store_user_location
is always "/" and also, the functions after_sign_out_path_for
and after_sign_out_path_for
are not overridden and my code for them was never reached. 但是
store_user_location
的输出始终为“ /”,并且函数after_sign_out_path_for
和after_sign_out_path_for
不会被覆盖,并且我的代码也从未到达过。
I added after_sign_out_path_for
also in application_controller as follows: 我还在application_controller中添加了
after_sign_out_path_for
,如下所示:
def after_sign_in_path(resource_or_scope)
puts stored_location_for(resource_or_scope) || super
end
How can I fix this problem? 我该如何解决这个问题?
Try using request.referrer
to the sessions_controller.rb
or application_controller.rb
尝试对
sessions_controller.rb
或application_controller.rb
使用request.referrer
You can access referer path using request.referer
您可以使用
request.referer
访问引荐路径
Like below 像下面
protected
def after_sign_in_path_for(resource_or_scope)
stored_location_for(resource_or_scope) || request.referrer
end
def after_sign_out_path_for(resource_or_scope)
stored_location_for(resource_or_scope) || request.referrer
end
See the devise wiki 参见devise Wiki
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.