2016-04-15 99 views
2

我有設計寶石授權的用戶模型。我想補充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,在這之後得到的輸出是什麼 –

回答

-1

確保你」已經有一個方法,你的AplicationController像如下

def after_sign_in_path_for(resource) 
    resource.next_step 
end 

Next_step是其創建過程中在記錄中存儲的屬性,你可以決定硬代碼在這裏一些其他的路徑。

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 

嘗試在代碼中添加其他條件。這對我有效。我錯過了這樣的事情。

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 

希望這可以幫助別人。歡呼:)

相關問題