vendredi 8 février 2019

Rails: Devise after_sign_in_path for multiple resources routes to root

I am working on a library catalogue app, which authenticates admins and users. Somehow I am unable to set the after_sign_in_path_for, since login routes now to the public root, instead of the root of each namespace.

I had already implemented an admin namespace and admin authentication and everything was working. In order to customize the sign_out path I had added:

# 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

I had already included the user scope in this section, even if I had not implement the resource yet. Everything was working.

Now I want to add a user resource and collection namespace where the catalogue is placed. Since I now need to specify the upon sign_in redirect, I completed the application_controller.rb with:

# 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

Somehow I am now redirected to the main root instead of collection_opac_path or admin_root_path. The later was working before defining the after_sign_in_path.

In my route.rb I have the following entries:

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'

rake routes gives:

     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

What am I doing wrong? How can I get this to work?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire