![](/img/trans.png)
[英]Rails: method date_select how can I include :class for each select box in the dom?
[英]how can i include a variable at each array in rails?
我有用户模型,我希望将用户列表作为json,我希望每个用户length of name
以及响应的length of name
但是我不知道如何做到这一点? 我尝试如下所示,它导致了double render
错误。 如果我从循环中获取response_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”}}]
尝试:
@users = User.all
all_users = Array.new
@users.each do |user|
all_users << {"id" => user.id, "name" => user.name,"length" => user.name.size}
end
respond_to do |format|
format.json { render json: all_users.to_json }
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.