Rails 4 - 设计:return_to previous url

时间:2016-05-30 12:14:16

标签: ruby-on-rails ruby session devise

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

如何让此重定向工作?

1 个答案:

答案 0 :(得分:0)

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