我有这种类型的json
[{"width":308,"height":248,"fill":"#fff","radius":0,"blendMode":"normal","rotate":0,"strokeWidth":1,"stroke":"#b9b9b9","type":"rectangle","x":84,"y":46},{"width":260,"height":150,"fill":"white","blendMode":"normal","rotate":0,"strokeWidth":1,"stroke":"#b9b9b9","type":"circle","x":497,"y":104},{"width":69,"height":51,"fill":"#fff","radius":0,"blendMode":"normal","rotate":0,"strokeWidth":1,"stroke":"#b9b9b9","type":"rectangle","x":149,"y":112}]
我尝试以json格式写入数据库,但它提供了
{"objects":[[{"width":308,"height":248,"fill":"#fff","radius":0,"blendMode":"normal","rotate":0,"strokeWidth":1,"stroke":"#b9b9b9","type":"rectangle","x":84,"y":46},{"width":260,"height":150,"fill":"white","blendMode":"normal","rotate":0,"strokeWidth":1,"stroke":"#b9b9b9","type":"circle","x":497,"y":104},{"width":69,"height":51,"fill":"#fff","radius":0,"blendMode":"normal","rotate":0,"strokeWidth":1,"stroke":"#b9b9b9","type":"rectangle","x":149,"y":112}]]}
控制器
def new
@plan = Plan.new
@restaurant = Restaurant.find(params[:restaurant_id])
@plans = @restaurant.plans
respond_to do |format|
format.html
format.json { render json: @plans.distinct.pluck(:objects) }
end
end
def plan_params
params.require(:plan).permit(:restaurant_id, objects: [ :width, :height, :fill, :radius, :blendMode, :rotate, :strokeWidth, :stroke, :type, :x, :y])
end
我需要一种json
{"objects":[{...}]}
更新
class Restaurant < ApplicationRecord
has_many :plans, dependent: :destroy
end
class Plan < ApplicationRecord
belongs_to :restaurant
end
答案 0 :(得分:0)
您可以将代码更改为以下内容,以获得您想要的json
@plans.distinct.pluck(:objects).flatten