CarrierWave full url for default image

swaroopsm picture swaroopsm · Jan 12, 2015 · Viewed 10.8k times · Source

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?

Answer

San picture San · Jan 13, 2015

[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.