Rails:设计 after_sign_in_path 用于多个资源路由到根目录

Rails: Devise after_sign_in_path for multiple resources routes to root

我正在开发一个图书馆目录应用程序,它可以对管理员和用户进行身份验证。我无法设置 after_sign_in_path_for,因为现在登录路由到 public 根目录,而不是每个命名空间的根目录。

我已经实施了管理员命名空间和管理员身份验证,一切正常。为了自定义 sign_out 路径,我添加了:

# Overwriting the sign_out redirect path method
def after_sign_out_path_for(resource_or_scope)
  if resource_or_scope == :user
    collection_path
  elsif resource_or_scope == :admin
    new_admin_session_path
  else
    root_path
  end
end

我已经在本节中包含了用户范围,即使我还没有实现资源。一切正常。

现在我想在放置目录的地方添加一个用户资源和集合命名空间。因为我现在需要指定 sign_in 重定向,所以我完成了 application_controller.rb :

# Overwriting the sign_in redirect path method
def after_sign_in_path_for(resource_or_scope)
  if resource_or_scope == :user
    collection_opac_path
  elsif resource_or_scope == :admin
    admin_root_path
  else
    root_path
  end
end

不知何故,我现在被重定向到主根而不是 collection_opac_pathadmin_root_path。后者在定义 after_sign_in_path.

之前工作

在我的 route.rb 中,我有以下条目:

devise_for :users
devise_for :admins

namespace :collection do
  match '/',                          :to => 'opac#home'
  match '/opac',                      :to => 'opac#opac', :as => :opac
  root :to => 'opac#home'
end

namespace :admin do
  ...
  root :to => 'pages#pageadmin'
end

root :to => 'pages#manifesto'

管理员已在管理员命名空间的所有控制器上进行身份验证。用户在集合命名空间中的 opac 控制器的 opac 操作上进行了身份验证。

rake routes 给出:

     collection    /collection(.:format)       collection/opac#home
collection_opac    /collection/opac(.:format)  collection/opac#opac
collection_root    /collection(.:format)       collection/opac#home

     admin_root    /admin(.:format)            admin/pages#pageadmin
           root    /                           pages#manifesto

我做错了什么?我怎样才能让它工作?

提前致谢!

更新

我认为指定用户登录表单放在 collection/home 视图中很重要,提交和登录后应重定向到 collection/opac,而管理员登录表单在尝试时出现访问管理命名空间的根。

我补充了:

def after_sign_in_path_for(resource)
 stored_location_for(resource) ||
  if resource.is_a?(User)
    collection_opac_path
  elsif resource.is_a?(Admin)
    admin_root_path
  else
    super
  end
end

调整 devisegem/lib/devise/controllers/helpers.rb 中指定的内容,现在条件路由有效。