假设我有一个User模型,它有一个username和一个full_name。我想使用Active Model Serializer呈现一个用户对象,并且只根据current_user是空还是登录来呈现特定的参数。因为我也在使用pundit,所以我想知道是否有一种方法可以使用pundit来做到这一点。
例如:
用户为nil
{id:1, username: 'foo'}用户为logged in
{id:1, username: 'foo', full_name: 'bar'}发布于 2014-06-28 01:36:24
为什么不直接检查current_user在User序列化程序中是否为nil。不确定您使用的是哪个版本,但您可以使用0.9版本执行此操作
class UserSerializer < ActiveModel::Serializer
attributes :id, :username, :full_name
def filter(keys)
if scope.blank? #current_user is not logged in
keys - [:full_name]
else
keys
end
end
endhttps://stackoverflow.com/questions/24455853
复制相似问题