I am using CarrierWave for image uploads on a rails-api application which in turn is consumed by a backbone.js
client app. I notice that when there is no default image available it returns /assets/default.png
. It in turn has to be http://dev-server:3000/assets/default.png
. Here are my configuration settings:
# config/environments/development.rb
CarrierWave.configure do |config|
config.asset_host = "http://dev-server:3000"
end
# Image Uploader
....
def default_url
"/assets/default.png"
end
Where am I going wrong?
[Edit (updated answer)]
Updating my answer to setup asset_host
in rails config.
Rails.application.configure do
.
.
config.asset_host = 'http://dev-server:3000'
end
Then you can use asset_url
method or image_url
method of the helper. Since this is an image, I would recommend placing the image in app/assets/images
folder and use image_url
.
ActionController::Base.helpers.image_url("default.png")
This will give you the following URL:
http://dev-server:3000/images/default.png
You can try it in the console.
[Old Answer]
Looking at Carrierwave Documentation, it seems like your default_url
method should look like this (Carrierwave does not automatically perpend asset_host to the default url):
def default_url
ActionController::Base.helpers.asset_path("default.png")
end
I am assuming that asset_host
is setup properly in your Rails configuration. If not, please do so.
I'm using Rails 5 API and Carrierwave too.
A lucky guess got it working for me.
I have a file inside app/uploaders/image_uploader.rb
.
The asset_host
configuration is set inside this file (at least it works for me):
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
...
def asset_host
return "http://localhost:3000"
end
end
Hope this works for anyone else in the future having this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With