简体   繁体   English

Rails-Devise-登录或注销后重定向到当前页面

[英]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_forafter_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.rbapplication_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.

相关问题 登录或注销后进行设计重定向不适用于Rails页面缓存 - Devise redirect after login or logout doesn't work with Rails page caching Rails在登录后设计重定向 - Rails Devise redirect after login Rails / Devise登录后如何重定向到特定页面 - Rails/Devise How to redirect to a specific page after login 使用Devise登录后重定向到上一页 - Redirect to previous page after login with Devise Rails / Devise /链接到登录页面,并向其传递重定向路由,以在成功登录后使用 - Rails / Devise / Link to login page and pass it a redirect route to use after successful login 注销和登录(sign_out,sign_in)后,Rails,devise,SPA重定向到自定义URL(查询字符串中包含参数) - Rails, devise, SPA, redirect to custom url (with param in query string) after logout and login (sign_out, sign_in) 使用devise在Rails 3中发送密码重置说明后,如何将用户重定向到登录页面 - How to redirect the user to login page after sending password reset instructions in rails 3 using devise Rails 和 Devise:成功登录后如何将用户重定向回请求的页面? - Rails and Devise: How to redirect user back to the requested Page after successful login? Rails&Devise:注销和登录不适用于localhost - Rails & Devise: Logout and login not working on localhost Rails&Devise-在登录和注销时获得双重提示 - Rails & Devise - Getting double notices on login and logout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM