[英]Rails Devise - Pass URL to login
Is there a way for me to pass an URL to the Devise login page, so that when a user logs in, he/she is redirected back to that URL? 我是否可以通过一种方法将URL传递到Devise登录页面,以便用户登录时将他/她重定向回该URL?
Something like: 就像是:
/login?passthru=/somethingawesome
Or is it better to set a session variable? 还是设置会话变量更好?
Here's what I did 这就是我所做的
1) In your template set up your sign_in login as follows: Im passing request.fullpath here as an example you can replace this with whatever you want. 1)在您的模板中,设置您的sign_in登录,如下所示:在此处传递request.fullpath作为示例,您可以将其替换为所需的内容。
<%= link_to "Log in", new_user_session_path(:passthru => request.fullpath %>
2) Next modify ApplicationController as follows: we add a before_filter which sets passthru in the session if it exists. 2)接下来,如下修改ApplicationController:我们添加一个before_filter,它会在会话中设置passthru(如果存在)。 Then we override after_sign_in_path_for to look in the session for passthru. 然后,我们覆盖after_sign_in_path_for以在会话中查找通过。 If its not there it will default to root_path. 如果不存在,则默认为root_path。 As long as you handle logins with params consistently everywhere this should work. 只要您在任何地方都以一致的方式处理使用登录名的登录,这应该可以工作。 Although it might need some tweaking. 尽管可能需要进行一些调整。
before_filter :store_location
def store_location
session[:passthru] = params[:passthru] if params[:passthru]
end
def redirect_back_or_default(default)
session[:passthru] || root_path
end
def after_sign_in_path_for(resource_or_scope)
redirect_back_or_default(resource_or_scope)
end
Have a method to store the redirect location and a method to access the stored redirect location in application_controller
: 在application_controller
有一个存储重定向位置的方法和一个访问存储的重定向位置的application_controller
:
def store_location(path)
session[:return_to] = request.request_uri || path
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
Override the after_sign_in_path_for method to redirect the user to the desired location: 覆盖after_sign_in_path_for方法以将用户重定向到所需位置:
def after_sign_in_path_for(resource_or_scope)
redirect_back_or_default(resource_or_scope)
end
Devise Wiki: How To: Redirect to a specific page on successful sign in out Devise Wiki:如何: 成功注销后重定向到特定页面
By the way, the above method is untested and you should test it. 顺便说一句,上述方法未经测试,您应该对其进行测试。
Update for Newer Versions (Ruby 2.2.0, Rails 4.2.0, Devise 3.2.4): 较新版本(Ruby 2.2.0,Rails 4.2.0,Devise 3.2.4)的更新:
Application_Controller.rb Application_Controller.rb
before_action :store_location
private
def store_location
session[:requestUri] = params[:requestUri] if params[:requestUri].present?
end
Devise Sessions_Controller.rb 设计Sessions_Controller.rb
# Custom After Sign in Path
def after_sign_in_path_for(resource_name)
if session[:requestUri]
session.delete(:requestUri)
else
super
end
end
View xxxx.html.erb 查看xxxx.html.erb
<%= link_to ('Get This'), some_require_auth_path(@something.id, requestUri: request.fullpath), class: 'button' %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.