Carrierwave
has a built-in fallback mechanism for default image
Update your default_url
method in AvatarUploader
as below:
def default_url
ActionController::Base.helpers.asset_path("fallback/" + [version_name, "my_fallback.jpg"].compact.join('_'))
end
where change fallback/
to your desired folder path.
This way, when an avatar
is not uploaded for a particular user then my_fallback.jpg
would be used as fallback image.
Refer to section Providing a default URL in Carrierwave Documentation.
when I update the attributes of the file_name column, the uploader kicks in and my_fallback.jpg is overridden by a random string from my random_string method!
This happens because you have overridden filename
method in AvatarUploader
which gets called every time an image is uploaded. If you notice, its calling random_string
method in it. Hence, you get a random string
as your filename.
UPDATE
As per the chat session with OP, if an avatar is not uploaded for a user then a default image should be shown. I suggested the following helper :
module ApplicationHelper
def display_avatar(user)
unless user.avatar.nil?
image_tag(user.avatar.file_name)
else
image_tag("/path/to/fallback.jpg")
end
end
## ...
end
Use this helper method in views to display avatar image appropriately.