简体   繁体   English

Rails 4 - 设计:return_to previous url

[英]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. 在Rails 4.2.4中,我使用了Devise gem(3.5.6,3.5.2,3.2.4),并尝试使其适用于特定场景。

When a user tries to access the user_profile_url(:anchor=>'section1') page from an email, it asks him to login; 当用户尝试从电子邮件中访问user_profile_url(:anchor=>'section1')页面时,它会要求他登录; after login, it leads to users_home_path . 登录后,它会导致users_home_path If he is already logged in, then it redirects to user_profile_url page(#section1). 如果他已经登录,则会重定向到user_profile_url页面(#section1)。

I want to redirect to user_profile_url(:anchor=>'section1') as soon as the session is created (clicked from email link)? 我想在创建会话后立即重定向到user_profile_url(:anchor=>'section1') (从电子邮件链接中点击)? I have tried to fix it by modifying after_sign_in_path_for(resource) method, but it is not working. 我试图通过修改after_sign_in_path_for(resource)方法来修复它,但它无法正常工作。

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

In html views, 在html视图中,

<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 您需要为会话[:return_to]编写代码,如下面的代码段所示

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 资料来源: https//github.com/plataformatec/devise/wiki/How-To : -Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update

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

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