更新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
,这样我就可以像为什么我收到的错误是name
是nil
。但这种方法一直有效?!
我试图将其更改为current_user.blank?
这是可行的,但接下来的行中出现相同的错误(如预期),说email
是nil
。
我该怎么办?
它返回什么? 'current_user.blank?'或者** true **或者** false ** – 2013-03-22 06:47:26
当使用cancan时,你可以在控制器中执行load_and_authorize_resource,这也确保了身份验证的发生,你使用这个助手吗?因为它看起来像你没有登录的用户。 – Matt 2013-03-22 09:07:10
'current.user.blank?'返回** true **。 'load_and_authorize_resource'和下面的答案的新缩进一起工作。这可能是别人的答案。谢谢! – JanDintel 2013-03-22 12:45:40