Mladen Danic
Mladen Danic

Reputation: 3669

Display a custom url for a carrierwave uploaded image in Rails

I am uploading some images in a folder outside of the public folder. I create a preview version of the image on upload. In my uploader I have:

  def store_dir
    "#{Rails.root}/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :preview do
    process :resize_to_fit => [500, 500]
  end

This works well, I have no problems with it, but now when my JSON api sends the model to my front app, the url parameter for both the image and the preview version is set to the exact file path of the file on my machine. I obviously don't want that.

Is there a way I could store a custom custom url for the image in a similar way as with setting the store_dir, so that I can later create a controller method that would serve these images?

Upvotes: 0

Views: 1379

Answers (1)

Radek
Radek

Reputation: 202

I don't remember any simple solution for that, but I think you can achieve that by specify the url in JSON by build it manually. For example you can use gem like RABL, active_model_serializers or build method in model eg. def to_public_json which has a custom url for the image.

Upvotes: 1

Related Questions