[英]CarrierWave does not return url after file upload
我正在嘗試使用rails + Carrierwave上傳文件后獲取文件url。
這是我的控制器:
class PhotosController < ApplicationController
before_action :check_authorization!
skip_before_action :verify_authenticity_token, only: :create
def index
render json: Photo.all
end
def create
@photo = Photo.create(photo_params)
if @photo.valid?
render json: @photo
else
render json: { errors: @photo.errors.full_messages }, status: 422
end
end
private
def photo_params
params.permit(:photo, :description)
end
end
但是在文件上傳后,我得到的是:
{
"id": 12,
"photo": "#<ActionDispatch::Http::UploadedFile:0x00000004c08c18>",
"description": "test",
"created_at": "2018-07-22T18:01:01.699Z",
"updated_at": "2018-07-22T18:01:01.699Z"
}
我已經嘗試過merge
並嘗試獲取帶有photo屬性的url,但是我遇到的問題是該屬性photo
是一個String。
根據Carrierwave文檔 ,您可以使用.url
檢查URL:
@photo.photo.url
第二個“照片”是配置用於保存文件的字段。 另外,如果需要路徑,可以使用: .current_path
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.