0
iam carierwaveを使用してマルチパート/フォームデータを投稿しています。 これは私のスクリプトですアップロードされたファイルパスにcarierwaveがヌルになる
#imagepath model
class Imagepath < ActiveRecord::Base
belongs_to :imagepost
attr_accessor :path
mount_uploader :path, ImagepathUploader
end
#imagepost model
class Imagepost < ActiveRecord::Base
belongs_to :user
has_many :imagepaths
has_many :imagecomments
has_many :imagelikes
attr_accessor :imagepath_data
# attr_accessor :path
end
#imagepost controller post method
# POST /imageposts
def create
@imagepost = Imagepost.new(imagepost_params)
if @imagepost.save
params[:imagepost][:imagepath_data].each do |file|
@imagepost.imagepaths.create!(:path => file)
end
render json: @imagepost, status: :created, location: @imagepost
else
render json: @imagepost.errors, status: :unprocessable_entity
end
end
#imagepost_params for post_params
def imagepost_params
params.require(:imagepost).permit(:title, :description, :user_id, :imagepath_data => [])
end
データを投稿するカール使用してIAM
curl
-F "imagepost[imagepath_data][]=c4ewt.JPG"
-F "imagepost[imagepath_data][]=border-image.png"
-F "imagepost[title]=asasassasa"
-F "imagepost[description]=uhuhuhuhuhuhuh"
-F "imagepost[user_id]=5" localhost:3000/imageposts
投稿が仕事ですが、投稿しないの後、私は私のImagePathテーブルからパス行を取得するnullである:(
感謝。私もシリアル化:アバター、私のimagepathsモデルのJSONを追加し、今働いた。私はsqliteを訴える – cahyowhy