设计辅助方法(current_user)返回nil

时间:2013-03-22 03:40:16

标签: ruby-on-rails devise

更新HAML(从4.0.0到4.0.1)后,我的Devise助手方法出错了。这可能会引发错误,不确定。

诸如user_signed_in?之类的Devise辅助方法正在运行(我在我的视图中没有它),但方法current_user不是。在undefined method name for nil期间,此错误引发current_user.name.blank?

这是我的应用程序控制器:

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):

!!! 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在better_errors的实时shell中返回nil,因此我可以查看为什么我收到namenil的错误。但这种方法总能奏效吗?!

我尝试将其更改为有效的current_user.blank?,但在下一行中出现相同的错误(如预期),emailnil

我该怎么办?

1 个答案:

答案 0 :(得分: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)