16

I have user model for authorization with devise gem. I want to add after_sign_in_path method:

# application_controller.rb

protected
# redirecting to appropriate url based on role
  def after_sign_in_path_for(resource)
    if current_user.has_role?(:admin)
      dashboard_path
    elsif current_user.has_role?(:student)
      root_path
    end
  end
Whenever I try to sign in I get this error:

undefined method `user_url' for #<Devise::SessionsController:0x007fb89b5b00a8> Did you mean? course_url
I don't know why it says 'did you mean? course_url. But I have course model. And here are my routes:

authenticate :user do

    resources :feeds, only: [:index]
    resources :courses, only: [:index, :show]    
    # etc...

  end

Also here is the code it points me:

if options.empty?
            recipient.send(method, *args)
  else
            recipient.send(method, *args, options)
  end

and first line of log:

actionpack (4.2.4) lib/action_dispatch/routing/polymorphic_routes.rb:220:in `polymorphic_method' 

Whenever I commend after_sign_in_path_for I am able to sign in. If I comment contents of after_sign_in_path_for but leave empty after_sign_in_path_for method, I also get this error.

EDIT: I tested that I am not also signed in, not just not redirected. I think error happens right in the call after_sign_in_path_for, not in the redirect_to or whatever. Probably it has to do something with resource.

EDIT2: here are my rake routes:

  new_user_session GET    /users/sign_in(.:format)          devise/sessions#new
            user_session POST   /users/sign_in(.:format)          devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)         devise/sessions#destroy
           user_password POST   /users/password(.:format)         devise/passwords#create
       new_user_password GET    /users/password/new(.:format)     devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)    devise/passwords#edit
                         PATCH  /users/password(.:format)         devise/passwords#update
                         PUT    /users/password(.:format)         devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)           registrations#cancel
       user_registration POST   /users(.:format)                  registrations#create
   new_user_registration GET    /users/sign_up(.:format)          registrations#new
  edit_user_registration GET    /users/edit(.:format)             registrations#edit
                         PATCH  /users(.:format)                  registrations#update
                         PUT    /users(.:format)                  registrations#update
                         DELETE /users(.:format)                  registrations#destroy
       user_confirmation POST   /users/confirmation(.:format)     devise/confirmations#create
   new_user_confirmation GET    /users/confirmation/new(.:format) devise/confirmations#new
                         GET    /users/confirmation(.:format)     devise/confirmations#show
              admin_root GET    /                                 rails_admin/main#dashboard
            student_root GET    /                                 feeds#index
                   feeds GET    /feeds(.:format)                  feeds#index
                 courses GET    /courses(.:format)                courses#index
                  course GET    /courses/:id(.:format)            courses#show
                 schools GET    /schools(.:format)                schools#index
                  school GET    /schools/:id(.:format)            schools#show
            universities GET    /universities(.:format)           universities#index
              university GET    /universities/:id(.:format)       universities#show
             rails_admin        /admin                            RailsAdmin::Engine
                         POST   /graphql(.:format)                graphql#create
    landing_confirmation GET    /landing/confirmation(.:format)   landing#confirmation
   landing_access_denied GET    /landing/access_denied(.:format)  landing#access_denied
                    root GET    /                                 landing#index

EDIT3: here is my github repo:

https://github.com/yerassyl/nurate

21
  • Are you able to get current_user in after_sign_in ? Commented Apr 15, 2016 at 11:49
  • @ChakreshwarSharma, even if i comment contents of after_sign_in_path_for, I get this error.
    – yerassyl
    Commented Apr 15, 2016 at 11:51
  • remove the contents of method & write logger.info("current_user="+current_user.to_json)super , what is the output you get after this Commented Apr 15, 2016 at 11:55
  • At first look, your routes doesn't look standard. I mean the way that devise suggested it. You should check the devise gem doc. Personally I would configure a classic devise routes and authenticate in controller with a before action. Commented Apr 15, 2016 at 11:57
  • @ChakreshwarSharma, undefined method super' for true:TrueClass Did you mean? suppress. or if I remove .super I got: undefined method to_model' for true:TrueClass Did you mean? to_yaml
    – yerassyl
    Commented Apr 15, 2016 at 12:00

6 Answers 6

48

I had the same issue (Rails 7). I fixed it this way:

Add :turbo_stream as a navigational format. This line goes in config/initializers/devise.rb.

config.navigational_formats = ['*/*', :html, :turbo_stream]

Devise issue github

6

This error is thrown when the after_sign_in_path_for method is redefined in your app and returns nil value. As per my best guest, the provided snippet

def after_sign_in_path_for(resource)
  if current_user.has_role?(:admin)
    dashboard_path
  elsif current_user.has_role?(:student)
    root_path
 end
end

is giving an error because none of the conditions are getting satisfied and hence the returned value is nil. To avoid such a case, you can always add an else condition which would get satisfied if no other does. Hence the below snippet should have worked (and will work for other users)

def after_sign_in_path_for(resource)
  if current_user.has_role?(:admin)
    dashboard_path
  elsif current_user.has_role?(:student)
    root_path
  else
   some_other_path || root_path
  end
end

Hope this helps someone. Cheers :)

1
  • That was it! Totally solved it for me. Thanks!
    – hcatlin
    Commented Aug 6 at 15:19
4

In config/devise.rb include this line:

config.navigational_formats = ['/', :html, :turbo_stream]

before rails s

This has done the trick for me.

1
  • Thanks. Doing `config.navigational_formats = ['/, :html'] was enough for me. It's worth to mention that I just kicked off a new project and haven't started implementing Hotwire yet. Commented Jun 19, 2022 at 21:09
3
def after_sign_in_path_for(resource)
 if current_user.has_role?(:admin)
  dashboard_path
 elsif current_user.has_role?(:student)
  root_path
 else
  root_path
 end
end

Try to add a else condition in your code. This worked for me. I missed something like this.

1

If you have the same error with Devise::RegistrationsController, the answers of Jishnu and antoniolulee also works fine.

You have to uncomment in config/initializers/devise.rb line says

config.navigational_formats = ['*/*', :html]

and add :turbo_stream like this

config.navigational_formats = ['*/*', :html, :turbo_stream]
-1

Make sure you've got a method on your AplicationController like follows

def after_sign_in_path_for(resource)
  resource.next_step
end

Next_step is an attribute stored in the record during its creation, you may decide to hard code here some other path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.