Devise SessionsController的未定义方法`user_url':创建

问题描述:

我有设计宝石授权的用户模型。我想补充after_sign_in_path方法:Devise SessionsController的未定义方法`user_url':创建

# 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或其他地方。可能它必须对资源做些事情。

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

EDIT3:这里是我的github回购:

https://github.com/yerassyl/nurate

+0

你能否在after_sign_in中获得current_user? –

+0

@ChakreshwarSharma,即使我评论after_sign_in_path_for的内容,我得到这个错误。 – yerassyl

+0

删除方法的内容并写入logger.info(“current_user =”+ current_user.to_json)super,在这之后得到的输出是什么 –

确保你”已经有一个方法,你的AplicationController像如下

def after_sign_in_path_for(resource) 
    resource.next_step 
end 

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

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 

尝试在代码中添加其他条件。这对我有效。我错过了这样的事情。

我敢肯定,错误必须已经解决,直到现在。今天我开始讨论它,为了提醒并让其他用户知道(如果有的话),我会在这里提供答案。

当您的应用重新定义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 

希望这可以帮助别人。欢呼:)