Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise stored_location_for(resource) returns nil

I read the few posts about troubleshooting stored_location_for here, but can't seem to figure it out and not sure how to troubleshoot.

I tried deleting my custom after_sign_in_path_for, but that didn't work either. My location is never getting saved, although as I understand it after each session/page update it should store the location. Do I need to through that in as a filter manually?

def after_sign_in_path_for(resource)
    stored_location_for(resource) ||
      if resource.is_a?(Account)
        add_quote_to_account(resource)
        if resource.applications.any?
          edit_application_path(resource.applications(true).last)
        else
          root_path
        end
      else
        super
      end
  end
like image 277
Elijah Murray Avatar asked Dec 06 '25 02:12

Elijah Murray


1 Answers

May be you are not storing the location where you want to redirect_to after signing in with devise. Devise provides two methods - store_location_for and stored_location_for https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/store_location.rb

Suppose your user model is named "user" then

  1. A call to store_location_for(:user, my_desired_path) in your controller will store the url "my_desired_path" in session with key "user_return_to". Basically this method will simply do this - session["user_return_to"] = my_desired_path. Probably you are missing this. I have a booking controller and a "login" action which stores the checkout location for booking in booking controller and then displays a login form for users in rendered view -

    def login
        my_desired_path = url_for(controller: 'bookings', action: 'checkout')
        store_location_for(:user, my_desired_path)
    end
    
  2. Now you can use stored_location_for(:user) to retrieve my_desired_path from your session. So to say, a call to stored_location_for(:user) will return "my_desired_path.

  3. Now if you use stored_location_for in your custom after_sign_in_path_for(:user) then it shall return "my_desired_path".

Additional Point -

A call to stored_location_for(:user) returns session[:user_return_to] but also clears this session after returning the value if your redirect format is navigational format. So a second call to stored_location_for(:user) will not return my_desired_path. And sometimes this is how we want our application to behave. On contrary, if your redirect format is not navigational format then session wont be cleared and a second sign-in will again redirect to same "my_desired_path".

Sometimes you want to redirect to different locations in signing-in from different pages in your application. Suppose, you want to redirect to "\X" on page A and to "\Y" on page B. Now follow these steps -

  1. User in on page A - store_location_for(:user, "\X") is saved in session.
  2. Application provides a sign-in form but User does not sign-in and just browse here and there in your application.
  3. User is on page B and perform a sign-in expecting to land on "\Y" but unexpectedly lands to "\X".

So take care of it in your application. This scenario is more likely to occur in applications which uses AJAX for signing-in.

like image 108
Vijay Meena Avatar answered Dec 08 '25 16:12

Vijay Meena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!