Full CarrierWave URL for default image

I am using CarrierWave to load images to rails-api , which in turn is consumed by the client application backbone.js

. I noticed that when there is no default image, it returns /assets/default.png

. This, in turn, should be http://dev-server:3000/assets/default.png

. Here are my config 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?

+3


source to share


4 answers


[Edit (updated answer)]

Update my answer to setting it up asset_host

in rails config.

Rails.application.configure do
  .
  .
  config.asset_host = 'http://dev-server:3000'
end

      

Then you can use the helper method asset_url

or image_url

methods. Since this is an image, I would recommend placing the image in a folder app/assets/images

and using 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 on the console.

[Old answer]

Looking at the Carrierwave Documentation , it seems like your method default_url

should look like this (Carrierwave does not automatically carry the activation to the default url):

def default_url
  ActionController::Base.helpers.asset_path("default.png")
end

      

I am assuming I am asset_host

properly configured in the Rails config. If not, do it.

+5


source


I am using Rails 5 API and Carrierwave too.

A lucky guess made it work for me.

I have a file inside app/uploaders/image_uploader.rb

.



The config is asset_host

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

      

Hopefully this works for someone else in the future having this problem.

+6


source


I would recommend some of your ideas:

I: Put the host in your zB development.rb environment

config.asset_host = 'http://localhost:3000'

      

II: create a file in config / initializers / carrierwave.rb

# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
  config.storage = :file
  config.asset_host = ActionController::Base.asset_host
end

      

III: edit your user:

def default_url
    "#{asset_host}/images/fallback/" + [version_name, "default.png"].compact.join('_')
  end

      

IV: RESTART your server

+1


source


I solved the problem by looking at the code carrierwave

. This is what I ended up with:

  def default_url
    "#{asset_host}#{ActionController::Base.helpers.asset_path("default.png")}"
  end

      

Also make sure you include the configuration asset_host

in the appropriate environment files.

0


source







All Articles