![](/img/trans.png)
[英]Debugging Help: Ruby on Rails File Saving Failed with S3 and Carrierwave
[英]Ruby on Rails - Carrierwave not saving file
我正在嘗試使用carrierwave
上傳圖像以保存它,目前發送的參數僅將“ null”保存到關聯的行,任何見解都會得到應用:
我正在使用rails-api
( https://github.com/rails-api/rails-api )
#controller
def create
@picture = Picture.new(picture_params)
if @picture.save
render json: @picture, status: :created, location: @picture
else
render json: @picture.errors, status: :unprocessable_entity
end
end
class Picture < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
mount_uploader :image_url, ImageUploader
end
class ImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
圖式
create_table "pictures", force: true do |t|
t.string "image_url"
t.integer "imageable_id"
t.string "imageable_type"
t.datetime "created_at"
t.datetime "updated_at"
end
控制台輸出
Started POST "/pictures" for 127.0.0.1 at 2014-09-01 23:44:47 +0100
Processing by PicturesController#create as */*
Parameters: {"test.jpg"=>#<ActionDispatch::Http::UploadedFile:0x2591868 @tempfile=# <File:C:/Users/xxx/AppData/Local/Temp/RackMultipart20140901-5044-1eui2xj>, @original_filename="test.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"
test.jpg\"; filename=\"test.jpg\"\r\nContent-Type: image/jpeg\r\n">}
←[1m←[36m (0.0ms)←[0m ←[1mbegin transaction←[0m
←[1m←[35mSQL (1.0ms)←[0m INSERT INTO "pictures" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2014-09-01 22:44:47.289649"], ["updated_at", "2014-09-01 22:44:47.289649"]]
←[1m←[36m (509.0ms)←[0m ←[1mcommit transaction←[0m
Completed 201 Created in 514ms (Views: 2.0ms | ActiveRecord: 510.0ms)
上面的POST
不會發送imageable_id或類型,但是將這些屬性添加到帖子中並不能解決問題。
編輯:將以下內容添加到控制器允許其保存其他字段,但carrierwave仍不保存iamge地址
def picture_params
params.require(:picture).permit(:imageable_id, :imageable_type, :image_url)
end
我知道了:我正在使用POSTMAN,並且需要將標頭列出為picture[image_url]
,而不僅僅是名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.