api保存多个子记录仅保存一个缺少字段的红宝石

时间:2019-06-22 07:37:28

标签: ruby-on-rails ruby rails-api

进行一个具有父记录(即项目)和子记录(即照片)的api调用,可以很好地保存该项目记录,但是图像仅保存一个缺少字段的图像。

这是json im正在发送

{"item_category": "Books & Magazines", "item_condition": "Used",
"item_name": "Crushing it", "summary": "super awesome",
"price": 20, "active": true,"instant": 1,
"access_token": "p8Z-yZ1wRooBLsZj4yeS",
"photo_attributes":
[{"created_at": "2019-05-16 05:28:16.696408",
"image_file_name": "images.jpg",
"image_content_type": "image/jpeg","image_file_size": 257908,
"image_updated_at":"2019-05-21 15:20:55.390445"},
{"created_at": "2019-05-16 05:28:16.696408",
"image_file_name": "images.jpg",
"image_content_type": "image/jpeg","image_file_size": 257908,
"image_updated_at":"2019-05-21 15:20:55.390445"}
]}

这是api im调用

http://localhost:3000/api/v1/createitem

这是命令行的图像 enter image description here

如您所见,此项目已保存 enter image description here

如您所见,它仅拍摄了一张包含3个场的图像 enter image description here

this items.contoller.rb

 class Api::V1::ItemsController < ApplicationController
 def createitem
   @item = current_user.items.new(item_params)
   if @item.save 
     render json: @item, status: :ok
   else
     render json: { error: "Something went wrong", is_success: false}, status: 422
   end
 end
 def item_params
       params.require(:item).permit(:item_category, :item_condition,:item_name,:summary,:address,:price,:active, :instant, photo_attributes:[:created_at, :image_file_size, :image_updated_at, :image_file_name, :image_content_type])
  end
 end

这是item.rb模型

class Item < ApplicationRecord
enum instant: {Request: 0, Instant: 1}

belongs_to :user
has_many :photos
accepts_nested_attributes_for :photos

validates :item_category, presence: true
validates :item_condition, presence: true
end

我对此解决方案感到厌倦,但仍然没有保存任何照片 Rails 4 nested attributes not saving

这是更新items.controller.rb

class Api::V1::ItemsController < ApplicationController
 def createitem
   @item = current_user.items.build(item_params)
   @item.photos.each do |photo|
     photo.build
   end
   if @item.save 
     render json: @item, status: :ok
   else
     render json: { error: "Something went wrong", is_success: false}, status: 422
   end
 end
 def item_params
   params.require(:item).permit(:item_category, :item_condition,:item_name,:summary,:address,:price,:active, :instant, photos_attributes:[:created_at, :image_file_size, :image_updated_at, :image_file_name, :image_content_type])
  end
 end

2 个答案:

答案 0 :(得分:0)

首先,您的json应该是

{"item_category": "Books & Magazines", "item_condition": "Used",
"item_name": "Crushing it", "summary": "super awesome",
"price": 20, "active": true,"instant": 1,
"access_token": "p8Z-yZ1wRooBLsZj4yeS",
"photo_attributes":
[{"created_at": "2019-05-16 05:28:16.696408",
"image_file_name": "images.jpg",
"image_content_type": "image/jpeg","image_file_size": 257908,
"image_updated_at":"2019-05-21 15:20:55.390445"},
{"created_at": "2019-05-16 05:28:16.696408",
"image_file_name": "images.jpg",
"image_content_type": "image/jpeg","image_file_size": 257908,
"image_updated_at":"2019-05-21 15:20:55.390445"}
]}

然后第二次更新

def item_params
   params.require(:item).permit(:item_category, :item_condition,:item_name,:summary,:address,:price,:active, :instant, photos_attributes:[:created_at, :image_file_size, :image_updated_at, :image_file_name, :image_content_type])
end

使用

def item_params
   params.require(:item).permit(:item_category, :item_condition,:item_name,:summary,:address,:price,:active, :instant, photo_attributes:[:created_at, :image_file_size, :image_updated_at, :image_file_name, :image_content_type])

结束

并使用

创建方法
def create
 @item = current_user.items.new(item_params)
   if @item.save 
     render json: @item, status: :ok
   else
     render json: { error: "Something went wrong", is_success: false}, status: 422
   end
 end

答案 1 :(得分:0)

更改item_parms def解决了该问题

def item_params
 params[:item][:photos_attributes] = params[:photos_attributes]
 params.require(:item).permit(:item_category, :item_condition,:item_name,:summary,:address,:price,:active, :instant, photos_attributes:[:image_file_size, :image_updated_at, :image_file_name, :image_content_type])
end  

我从这里得到了答案

Rails 5.1 API - how to permit params for nested JSON object's attributes