[英]Rails and Devise: How to redirect user back to the requested Page after successful login?
I am using the idiom described in https://guides.rubyonrails.org/v2.3/action_controller_overview.html#other-ways-to-use-filters我正在使用https://guides.rubyonrails.org/v2.3/action_controller_overview.html#other-ways-to-use-filters中描述的成语
# /app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_filter do |controller|
redirect_to new_login_url unless controller.send(:logged_in?)
end
end
Now if the signing in process is successful, how现在如果登录过程成功,如何
EDIT: Also I get the following Error Message编辑:我也收到以下错误消息
uninitialized constant ApplicationController::LoginFilter
When I use the more elaborate solution suggested in 6.2 Other Ways to Use Filters
instead of the one above such that my Controller looks like this当我使用6.2 Other Ways to Use Filters
建议的更详细的解决方案而不是上面的解决方案时,我的 Controller 看起来像这样
# /app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :set_return_path, LoginFilter
def set_return_path
return if devise_controller?
session['user_return_to'] = request.url unless current_user
end
class LoginFilter
def self.filter(controller)
unless controller.send(:logged_in?)
controller.flash[:error] = "You must be logged in"
controller.redirect_to controller.new_login_url
end
end
end
end
Thanks谢谢
von Spotz冯·斯波茨
You can add a before_action
in the application_controller.rb
where you save the requested page url:您可以在application_controller.rb
中添加before_action
来保存请求的页面 url:
class ApplicationController < ActionController::Base
before_action :set_return_path
def set_return_path
return if devise_controller?
session['user_return_to'] = request.url unless current_user
end
end
And then redirect the user to this url after the successful sign-in:然后在登录成功后将用户重定向到这个url:
class SessionsController < Devise::SessionsController
def after_sign_in_path_for(resource)
return root_url if session['user_return_to'].blank?
session['user_return_to']
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.