简体   繁体   English

Rails 和 Devise:成功登录后如何将用户重定向回请求的页面?

[英]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现在如果登录过程成功,如何

  1. can I examine if it was, and b)我可以检查是否是,并且 b)
  2. how can I re-redirect the user to the requested controller action?如何将用户重新重定向到请求的 controller 操作?
  3. How do I do this login-process via AJAX and JSON?如何通过 AJAX 和 JSON 执行此登录过程?

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.

相关问题 成功进入DEVISE后,如何将用户重定向回以前需要登录的操作? - After a successful DEVISE login, how to redirect user back to previous action that required login? 如何在 devise 令牌验证中确认 email 后重定向到登录页面/或使用户登录成功 - How to redirect to login page/ or making user login successful after confirm email in devise token auth 如何在成功登录Rails设计时以及验证用户==“老师”后重定向页面 - How to redirect a page on successful sign in rails devise and after verify user== “teachers” Rails / Devise /链接到登录页面,并向其传递重定向路由,以在成功登录后使用 - Rails / Devise / Link to login page and pass it a redirect route to use after successful login 通过devise注册后如何将用户重定向回登录表单? - How to redirect user back to login form after signing up with devise? 使用devise在Rails 3中发送密码重置说明后,如何将用户重定向到登录页面 - How to redirect the user to login page after sending password reset instructions in rails 3 using devise Rails / Devise登录后如何重定向到特定页面 - Rails/Devise How to redirect to a specific page after login 用户请求新的确认后如何重定向到设计登录页面 - How to redirect to the Devise login page after user requests new confirmation 设计:成功登录后重定向到管理路径 - Devise: Redirect to Admin Path After Successful Login Rails:登录后如何重定向回原始页面 - Rails: How to redirect back to original page after login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM