0

当我尝试current_user通过设计访问...

def current_user 
    @user =  User.find(current_user.id)
    @user
end

并通过这个 RABL 模板在 JSON 中呈现它们的一些属性......

object @user
attributes :email, :username

我得到这个错误...

SystemStackError at /api/current_user
stack level too deep

我认为该错误是由于此用户模型中的某些内容引起的,我对其进行了一些修改以添加到用户名字段中。我在这里按照设计的说明添加了这个功能......

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :login, :username, :email, :password, :password_confirmation, :remember_me

  has_many :cart_items, dependent: :destroy

  # Virtual attribute for authenticating by either username or email
  # This is in addition to a real persisted field like 'username'
  attr_accessor :login

  def self.find_first_by_auth_conditions(warden_conditions)
      conditions = warden_conditions.dup
      if login = conditions.delete(:login)
        where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
      else
        where(conditions).first
      end
  end

end
4

1 回答 1

3

您正在从 current_user 中调用 current_user ...

def current_user 
   @user =  User.find(current_user.id)

                          ^^^^

然后它调用自己,然后它再次调用自己,一次又一次......直到你得到堆栈溢出

我不确定你似乎需要重写 current_user 方法。afaik Devise 已经为您做到了。你能告诉我那个教程中说要重写的地方def current_user吗?

于 2013-09-04T00:54:23.823 回答