简体   繁体   中英

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 .

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 . When user goes to buy now and is redirected to login screen, buy now url is a referer in this case.

Just copy the below code in your app/controllers/application_controller.rb file

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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