私はユーザーモデルを持っていて、ユーザーのリストをjsonとして欲しいのですがlength of name
、各ユーザーの応答と一緒に欲しいのですが、どうすればこれを行うことができますか?以下のように試してみましたが、double render
エラーになりました。ループからrespond_toを取得すると、何も返されません。助けてください。
@users = User.all
@users.each do |user|
length = user.name.count
respond_to do |format|
format.json { render json: @users.to_json(:include => [:length]) }
end
end
のような反応をしたい
[{"user":{"name":asgd、 "id":1、 "length": "4"}}、{"user":{"name":asdfg、 "id":2、 "length" :"5"}}]