【问题标题】:undefined method `user_url' for Devise SessionsController:create设计会话控制器的未定义方法“user_url”:创建
【发布时间】:2016-04-15 11:43:11
【问题描述】:

我有设计 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
每当我尝试登录时,都会收到此错误:

undefined method `user_url' for #<Devise::SessionsController:0x007fb89b5b00a8> Did you mean? 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 或其他中。可能它与资源有关。

EDIT2:这是我的 rake 路线:

  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

【问题讨论】:

  • 你能在 after_sign_in 中获取 current_user 吗?
  • @ChakreshwarSharma,即使我评论 after_sign_in_path_for 的内容,我也会收到此错误。
  • 删除方法的内容 & 写 logger.info("current_user="+current_user.to_json)super ,你得到的输出是什么
  • 乍一看,您的路线看起来并不标准。我的意思是设计建议的方式。您应该检查设计 gem 文档。就我个人而言,我会配置一个经典的设计路线并在控制器中使用之前的操作进行身份验证。
  • @ChakreshwarSharma,未定义的方法super' for true:TrueClass Did you mean? suppress. or if I remove .super I got: undefined method to_model' for true:TrueClass 你的意思是? to_yaml

标签: ruby-on-rails ruby-on-rails-4 devise


【解决方案1】:

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

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 条件,如果没有其他条件,它会得到满足。因此,下面的 sn-p 应该可以工作(并且将适用于其他用户)

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

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

【讨论】:

    【解决方案2】:
    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 条件。这对我有用。我错过了这样的事情。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题(Rails 7)。我是这样解决的:

      添加 :turbo_stream 作为导航格式。这一行进入 config/initializers/devise.rb。

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

      Devise issue github

      【讨论】:

        【解决方案4】:

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

        def after_sign_in_path_for(resource)
          resource.next_step
        end
        

        Next_step 是在创建过程中存储在记录中的一个属性,您可以决定在这里硬编码一些其他路径。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-26
          • 1970-01-01
          • 2012-10-12
          • 2012-07-18
          相关资源
          最近更新 更多