Devise SessionsController:create 的未定义方法“user_url”

yer*_*syl 16 ruby-on-rails devise ruby-on-rails-4

我有使用 devise gem 进行授权的用户模型。我想添加 after_sign_in_path 方法:

# 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
Run Code Online (Sandbox Code Playgroud)
每当我尝试登录时,都会收到此错误:

undefined method `user_url' for #<Devise::SessionsController:0x007fb89b5b00a8> Did you mean? course_url
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它会说“你是说吗?” course_url。但我有课程模型。这是我的路线:

authenticate :user do

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

  end
Run Code Online (Sandbox Code Playgroud)

这也是它指向我的代码:

if options.empty?
            recipient.send(method, *args)
  else
            recipient.send(method, *args, options)
  end
Run Code Online (Sandbox Code Playgroud)

和日志的第一行:

actionpack (4.2.4) lib/action_dispatch/routing/polymorphic_routes.rb:220:in `polymorphic_method' 
Run Code Online (Sandbox Code Playgroud)

每当我推荐 after_sign_in_path_for 时,我都可以登录。如果我评论 after_sign_in_path_for 的内容但保留空 after_sign_in_path_for 方法,我也会收到此错误。

编辑:我测试过我还没有登录,而不仅仅是没有重定向。我认为错误发生在调用 after_sign_in_path_for 中,而不是在 redirect_to 或其他地方。可能它必须与资源做一些事情。

EDIT2:这是我的耙子路线:

  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
Run Code Online (Sandbox Code Playgroud)

EDIT3:这是我的 github 存储库:

https://github.com/yerassyl/nurate
Run Code Online (Sandbox Code Playgroud)

小智 48

我有同样的问题(Rails 7)。我是这样修复的:

添加 :turbo_stream 作为导航格式。该行位于 config/initializers/devise.rb 中。

config.navigational_formats = ['*/*', :html, :turbo_stream]
Run Code Online (Sandbox Code Playgroud)

设计问题 github


Aak*_*sha 5

after_sign_in_path_for当您的应用程序中重新定义该方法并返回值时,会引发此错误nil。根据我最好的客人的说法,提供的片段

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
Run Code Online (Sandbox Code Playgroud)

给出错误,因为没有任何条件得到满足,因此返回值为nil。为了避免这种情况,您始终可以添加一个else条件,如果没有其他条件满足,该条件就会得到满足。因此,下面的代码片段应该有效(并且适用于其他用户)

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
Run Code Online (Sandbox Code Playgroud)

希望这对某人有帮助。干杯:)


Nun*_*sta -1

确保您有如下AplicationController方法

def after_sign_in_path_for(resource)
  resource.next_step
end
Run Code Online (Sandbox Code Playgroud)

Next_step是在创建记录期间存储在记录中的属性,您可能决定在此处硬编码其他路径。