当我尝试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