我正在尝试传递一个对象(当前用户)以在为集合呈现 json 时使用。
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @items }
format.json { render :json => @items.to_a.as_json(:user => current_user) }
end
但是,这似乎没有效果,因为 as_json 方法中的 options[:user] 为零。
JSON_ATTRS = ['id', 'created_at', 'title', 'content']
def as_json(options={})
# options[:user] is nil!
attributes.slice(*JSON_ATTRS).merge(:viewed => viewed_by?(options[:user]))
end
任何人都知道为什么这不起作用,或者可以建议一种更优雅的方式让 json 渲染器了解当前用户?
谢谢,魏