コレクションの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] が nil であるため、これは効果がないようです。
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レンダラーに現在のユーザーを認識させるよりエレガントな方法を提案できる人はいますか?
ありがとう、魏