![](/img/trans.png)
[英]Is there a better way cancel the current request and redirect to an error page in Rails?
[英]Redirect to current page rails
我在我的应用程序导航栏中有一个登录表单,我想知道是否有一种简单的方法可以在sign_in时将sign_in重定向到您当前所在的任何页面。 谢谢!
只要给出了引用者,您就可以redirect_to(:back)
。 但是,我建议你看看这个答案 ,因为那里提出的解决方案对于失败的登录和OAuth重定向等内容都具有弹性。
在Rails 5中,您应该使用redirect_back
并添加回退路径:
redirect_back(fallback_location: root_path)
如果您已经编写了代码来为您的用户创建会话,那么只需在sessions_controller中添加以下行:
redirect_to root_url, :notice => "Logged in!"
在这里,您可以输入您希望用户重定向到的任何路径,而不是root_url 。 但请确保你在routes.rb中有它
如果你考虑不成功的sign_in,你可以使用render
方法重新加载你的sign_in视图
来自Devise人的这个解决方案应该可以解决问题 ......
在ApplicationController
编写以下内容:
after_filter :store_location
def store_location
# store last url - this is needed for post-login redirect to whatever the user last visited.
return unless request.get?
if (request.path != "/users/sign_in" &&
request.path != "/users/sign_up" &&
request.path != "/users/password/new" &&
request.path != "/users/password/edit" &&
request.path != "/users/confirmation" &&
request.path != "/users/sign_out" &&
!request.xhr?) # don't store ajax calls
session[:previous_url] = request.fullpath
end
end
def after_sign_in_path_for(resource)
session[:previous_url] || root_path
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.