未定义的方法`user_url' for Devise SessionsController:create

时间:2016-04-15 11:43:12

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

我有用于设计授权的用户模型。我想添加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
&#13;
&#13;
&#13; 我不知道为什么说“你的意思?” course_url。但我有课程模型。以下是我的路线:

&#13;
&#13;
authenticate :user do

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

  end
&#13;
&#13;
&#13;

这也是它给我的代码:

&#13;
&#13;
if options.empty?
            recipient.send(method, *args)
  else
            recipient.send(method, *args, options)
  end
&#13;
&#13;
&#13;

和第一行日志:

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

每当我推荐after_sign_in_path_for我能够登录。如果我评论after_sign_in_path_for的内容但留下空的after_sign_in_path_for方法,我也会收到此错误。

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

EDIT2:这是我的佣金路线:

&#13;
&#13;
  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
&#13;
&#13;
&#13;

EDIT3:这是我的github回购:

&#13;
&#13;
https://github.com/yerassyl/nurate
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

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条件。这对我有用。我错过了这样的事情。

答案 1 :(得分:0)

我确定错误必须一直解决到现在为止。今天我为了提及并让其他用户知道(如果有的话),我会在这里提供答案。

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

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

答案 2 :(得分:-1)

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

def after_sign_in_path_for(resource)
  resource.next_step
end

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