1

I'm using Rails 3 with carrierwave gem.

I managed to removed the avatar image from my user model like this:

@user.remove_photo!

and it works perfectly. However, I want to set the picture url for that user back to the default_url (which is the image that every user has until they upload one).

Any ideas how?

--

Image display code:

<%= image_tag(@user.photo.send(:layout).url, :alt => @user.full_name, :class => 'photo large') %>

default_url code:

def default_url
    "default_photo.jpg"
  end
Substantial
  • 6,684
  • 2
  • 31
  • 40
content01
  • 3,115
  • 6
  • 41
  • 61
  • I would not set the default photo in the users avatar. I would have the image in your static assets and do an if statement that shows the default image if the user image doesn't exist. – fatfrog Nov 27 '13 at 22:35
  • Actually that's the only solution I can think of. I was wondering if the gem had any way to set the picture back to the default. – content01 Nov 27 '13 at 22:37
  • Paste your code for displaying the image, and paste your code for your def url – fatfrog Nov 27 '13 at 23:08
  • there you go @fatfrog – content01 Nov 27 '13 at 23:10

2 Answers2

0

From the docs:

https://github.com/carrierwaveuploader/carrierwave

In many cases, especially when working with images, it might be a good idea to provide a default url, a fallback in case no file has been uploaded. You can do this easily by overriding the default_url method in your uploader:

class MyUploader < CarrierWave::Uploader::Base
  def default_url
    "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  end
end

Or if you are using the Rails asset pipeline:

class MyUploader < CarrierWave::Uploader::Base
  def default_url
    ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  end
end
fatfrog
  • 2,118
  • 1
  • 23
  • 46
  • 1
    No, you didn't understand my issue. I already have that. The issue is go back to the default_url AFTER I deleted a photo. – content01 Nov 27 '13 at 22:57
  • You aren't following the code, it should be "/"+ [version_name, "default.png"].compact.join('_') – fatfrog Nov 27 '13 at 23:14
0

You need to force update the field in the database to null, after that carrierwave will use the default_url, you can do this by calling Model.where(<CONDITIONS>).update_all(photo: nil) or instance.update_column(photo: nil)

ragar90
  • 78
  • 9