我对使用map方法缺乏一些理解。
使用不带映射的json输出
format.json { render :json => @categories }给出了以下输出
[{"created_at":"2012-10-20T01:16:35+11:00","id":1,"name":"bathroom renovations","updated_at":"2012-10-20T01:16:35+11:00"}]使用带有映射的json
format.json { render :json => @categories.map(&:name) }给出了这个输出
["bathroom renovations"]我怎样才能让我的输出看起来像
[{"id":"1","name":"bathroom renovations"}]发布于 2012-11-03 14:14:24
我认为下面这些方法可能对你有用:
format.json {render json: @categories.map{|category| {:id => category.id, :name => category.name} }}https://stackoverflow.com/questions/13190459
复制相似问题