繁体   English   中英

Rails 4 - 设计:return_to previous url

[英]Rails 4 - Devise :return_to previous url

在Rails 4.2.4中,我使用了Devise gem(3.5.6,3.5.2,3.2.4),并尝试使其适用于特定场景。

当用户尝试从电子邮件中访问user_profile_url(:anchor=>'section1')页面时,它会要求他登录; 登录后,它会导致users_home_path 如果他已经登录,则会重定向到user_profile_url页面(#section1)。

我想在创建会话后立即重定向到user_profile_url(:anchor=>'section1') (从电子邮件链接中点击)? 我试图通过修改after_sign_in_path_for(resource)方法来修复它,但它无法正常工作。

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

在html视图中,

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

如何让这个重定向工作?

您需要为会话[: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

资料来源: 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