1
votes

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
2
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
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
Paste your code for displaying the image, and paste your code for your def url - fatfrog
there you go @fatfrog - content01

2 Answers

0
votes

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
0
votes

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)