Rails 4 - CarrierWave default_url doesn't work with object image
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def default_url
ActionController::Base.helpers.asset_path("fallback/" + [main, "default.png"].compact.join('_'))
end
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fit => [50, 50]
end
version :main do
process :auto_orient
process :resize_to_fit => [300, 360]
end
def auto_orient
manipulate! do |img|
img = img.auto_orient
end
end
end
I have a default image loaded in app / assets / images / fallback / main / default.png
This default_url corresponds to the standard pipelined resources described by the pearl of the carrier. It doesn't give me an error, but it doesn't show the default image only the broken image on screen. However, if there is a valid load, it shows the load correctly, so I know the thumb and core processes are running. The only part that breaks is default_url and I have no idea why. I am using rails 4 by the way. Any thoughts / ideas?
Thank!
My code in the view showing the image if loaded and the default broken url is
<div class="img-container">
<%= image_tag @user.image_url(:main).to_s %>
</div>
source to share
As far as I can see, you are using the "main" variable inside the method default_url
, but you should version_name
. If you want to use the "main" version directly, you must write the next one, asset_path("fallback/" + ["main", "default.png"].compact.join('_'))
or just write "fallback/main_default.png"
.
Also you have wrong paths:
- The default image is loaded in app / assets / images / backup / main/default.png
- default_url will generate the following path - 'fallback / main_default.png'
source to share