简体   繁体   中英

Rails 4 - Devise :return_to previous url

In Rails 4.2.4, I am using the Devise gem (3.5.6, 3.5.2, 3.2.4), and trying to make it work for a particular scenario.

When a user tries to access the user_profile_url(:anchor=>'section1') page from an email, it asks him to login; after login, it leads to users_home_path . If he is already logged in, then it redirects to user_profile_url page(#section1).

I want to redirect to user_profile_url(:anchor=>'section1') as soon as the session is created (clicked from email link)? I have tried to fix it by modifying after_sign_in_path_for(resource) method, but it is not working.

def after_sign_in_path_for(resource)
  session[:return_to] || users_home_path
end

In html views,

<div id="unsubscribe">
...
</div>

How can I get this redirect to work?

You need to write code for session[:return_to] like the below the snippet

def store_location
  # store last url - this is needed for post-login redirect to whatever the user last visited.
  return unless request.get? 
  if (request.path != "/users/sign_in" &&
      request.path != "/users/sign_up" &&
      request.path != "/users/password/new" &&
      request.path != "/users/password/edit" &&
      request.path != "/users/confirmation" &&
      request.path != "/users/sign_out" &&
      !request.xhr?) # don't store ajax calls
    session[:previous_url] = request.fullpath 
  end
end

Source: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update

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