ruby-on-rails 未定义设计会话控制器的方法“user_url”:创建

sigwle7e  于 2023-02-10  发布在  Ruby
关注(0)|答案(6)|浏览(114)

我有用户模型用于使用deviegem进行授权。我想添加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

每当我尝试登录时,我收到此错误:

undefined method `user_url' for #<Devise::SessionsController:0x007fb89b5b00a8> Did you mean? course_url

我不知道为什么它说'你是说?course_url.但是我有课程模型.这里是我的路线:

authenticate :user do

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

  end

这里也是它指向我的代码:

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

日志的第一行:

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

每当我对after_sign_in_path_for进行注解时,我都可以登录。如果我对after_sign_in_path_for的内容进行注解,但将after_sign_in_path_for方法留空,我也会收到此错误。
编辑:我测试了我没有登录,不仅仅是没有被重定向。我认为错误发生在调用after_sign_in_path_for之后,而不是redirect_to或其他地方。可能它与资源有关。
编辑2:这里是我的耙路线:

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:这里是我的github回购:

https://github.com/yerassyl/nurate
0x6upsns

0x6upsns1#

我也遇到了同样的问题(Rails 7),我是这样解决的:
添加:turbo_stream作为导航格式。这一行位于config/initializers/devise. rb中。

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

Devise issue github

gk7wooem

gk7wooem2#

在应用中重新定义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

正在给出错误,因为没有满足任何条件,因此返回值为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

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

b0zn9rqh

b0zn9rqh3#

在config/devies.rb中包含以下行:

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

钢轨前
这对我起了作用。

83qze16e

83qze16e4#

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

尝试在代码中添加一个else条件。这对我很有效。我错过了类似的内容。

lrl1mhuk

lrl1mhuk5#

如果您在使用Devise::RegistrationsController时遇到同样的错误,Jishnu和antoniolulee的答案也可以正常工作。
你必须在config/initializers/devie.rb中取消注解

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

并添加:turbo_stream,如下所示

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

czfnxgou6#

确保在AplicationController上有一个如下所示的方法

def after_sign_in_path_for(resource)
  resource.next_step
end

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

相关问题