简体   繁体   中英

CarrierWave does not return url after file upload

I'm trying to get the file url after uploading a file with rails + Carrierwave.

Here's my controller:

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

But after the file upload what I get is:

{
    "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"
}

I've tried with merge and trying to get the url with the photo attribute but the problem that I have is that the attribute photo is a String.

According to Carrierwave doc , you can check the URL with .url :

@photo.photo.url

Where the second "photo" is the field configured for save the file. Aditionally, if you need the path, you can use: .current_path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM