2013-03-22 65 views
0

更新HAML(從4.0.0到4.0.1)後,我的設計輔助方法出現錯誤。這可能引發錯誤,不確定。設計輔助方法(current_user)返回零

設計幫手方法,如user_signed_in?正在工作(雖然我沒有在我看來),但方法current_user不是。在current_user.name.blank?期間引發的此錯誤是undefined method name for nil

這是我的應用程序控制器:

class ApplicationController < ActionController::Base 
    before_filter :authenticate_user! 
    protect_from_forgery 


    rescue_from CanCan::AccessDenied do |exception| 
    flash[:alert] = "Access denied." 
    redirect_to items_path 
    end 

end 

這是我的應用程序視圖(HAML):在better_errors的外殼帶電

!!! 5 
%html 
    %head 
    %title Levitas secondhand 
    = stylesheet_link_tag 'application', media: 'all' 
    = javascript_include_tag 'application' 
    = csrf_meta_tags 

    %body 
    .container 
    %p{class: 'notice notice-block'}= notice 
    %p{class: 'alert alert-block'}= alert 

    - if signed_in?(:user) 
     Ingelogd als 
     %strong 
    - if current_user.name.blank? 
     = link_to current_user.email, current_user 
    - else 
     = link_to current_user.name, current_user 
     | 
    - if current_user.provider == nil  
     = link_to 'Account bewerken', edit_user_registration_path 
     | 
     = link_to "Uitloggen", destroy_user_session_path, method: :delete 
    - else 
     = link_to "Nieuwe gebruiker", new_user_registration_path 
     | 
     = link_to "Inloggen", new_user_session_path 
     | 
     = link_to "Inloggen met Facebook", user_omniauth_authorize_path(:facebook) 

current_user回報nil,這樣我就可以像爲什麼我收到的錯誤是namenil。但這種方法一直有效?!

我試圖將其更改爲current_user.blank?這是可行的,但接下來的行中出現相同的錯誤(如預期),說emailnil

我該怎麼辦?

+0

它返回什麼? 'current_user.blank?'或者** true **或者** false ** – 2013-03-22 06:47:26

+0

當使用cancan時,你可以在控制器中執行load_and_authorize_resource,這也確保了身份驗證的發生,你使用這個助手嗎?因爲它看起來像你沒有登錄的用戶。 – Matt 2013-03-22 09:07:10

+0

'current.user.blank?'返回** true **。 'load_and_authorize_resource'和下面的答案的新縮進一起工作。這可能是別人的答案。謝謝! – JanDintel 2013-03-22 12:45:40

回答

0

也許這只是一個格式問題與您的文章在這裏,但看起來你可能只是縮進錯了?也許這就是你的意思?

- if signed_in?(:user) 
    Ingelogd als 
    %strong 
    - if current_user.name.blank? 
    = link_to current_user.email, current_user 
    - else 
    = link_to current_user.name, current_user 
    | 
    - if current_user.provider == nil  
    = link_to 'Account bewerken', edit_user_registration_path 
    | 
    = link_to "Uitloggen", destroy_user_session_path, method: :delete 
- else 
    = link_to "Nieuwe gebruiker", new_user_registration_path 
    | 
    = link_to "Inloggen", new_user_session_path 
    | 
    = link_to "Inloggen met Facebook", user_omniauth_authorize_path(:facebook) 
+0

這個技巧!多麼愚蠢的錯誤。謝謝! – JanDintel 2013-03-22 12:42:09