简体   繁体   English

Rails的设计:登录用户后,将用户重定向到他想要的网址

[英]Rails devise: Redirect user after log in to the url he wanted to go

I need to redirect my users to the page they wanted to go, not to the previous one, for usability reasons. 由于可用性的原因,我需要将用户重定向到他们想要转到的页面,而不是上一个页面。

Right now If Im in page B and then want to go to page A that is log in required then user enters his credentials and is redirected to page B instead of page A . 现在,如果Im在页面B中 ,然后想转到需要登录的页面A ,则用户输入其凭据,然后重定向到页面B而不是页面A。

This has become a very serious usability problem in my app. 这已成为我应用程序中非常严重的可用性问题。

Imagine you are seeing a product and you click "Buy now", then you need to enter the log in details, right now after you log in you are redirected back to the product instead of the checkout form. 假设您正在查看产品,然后单击“立即购买”,那么您需要输入登录详细信息,登录后立即将您重定向到该产品,而不是结帐表格。

This is how my code looks like: 这是我的代码的样子:

class ApplicationController < ActionController::Base
    after_filter :store_location

    def store_location
      # store last url as long as it isn't a /users path
      session[:previous_url] = request.fullpath unless request.fullpath =~ /\/users/
    end

    def after_sign_in_path_for(resource)
      session[:previous_url] || root_path
    end
end

What you are looking for is a request.referer . 您正在寻找的是request.referer When user goes to buy now and is redirected to login screen, buy now url is a referer in this case. 当用户去立即购买并重定向到登录屏幕时,在这种情况下,立即购买 url是引荐来源。

Just copy the below code in your app/controllers/application_controller.rb file 只需将以下代码复制到您的app / controllers / application_controller.rb文件中

class ApplicationController < ActionController::Base      

      protected          
      protect_from_forgery with: :exception
      def after_sign_in_path_for(resource)
        dashboard_index_path
      end
end

I hope it'll help you.. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM