0

I am trying to set a default image for users when they sign up. They can change that if they prefer and otherwise a default image will be given. The input_html, however, doesn't seem to work?

How can I set a default image?

Current simple form for input:

<% default_picture = (image_path 'user.png') %>
<%= f.input :avatar, input_html: {value: '#{default_picture}'}, label: false %>
fool-dev
  • 7,671
  • 9
  • 40
  • 54
Pimmesz
  • 335
  • 2
  • 8
  • 29
  • How are you expecting this to work? Should the field be a file uploader, or are you selecting from a number of pre-defined avatars? This looks like a simple text field, is that what you want? – AJFaraday Mar 27 '18 at 09:16
  • If you want set default value for some attributes, you can use callback: https://stackoverflow.com/a/1551430/2640181 – barmic Mar 27 '18 at 09:18

2 Answers2

3

you can use before_create in your model, to set the default image.

before_create :set_default_avatar

def set_default_avatar
  # your code
end

and here other discussion regarding about your question, Rails - What is the best way to display default avatar if user doesn't have one?

  • Thanks for your reply! Does this work with CarrierWave as well? The avatars which are referenced (even when nil) have a lot of information so im not sure I can set the default image directly as described? – Pimmesz Mar 27 '18 at 13:06
  • yes it work well with carrierwave as well, you need to change `before_create` into `after_create` and add logic if image is nil than process with upload file, and also you can try follow this documentation from carrierwave https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-%22Upload%22-from-a-local-file , and also with carrierwave you can provide default url if the image is blank https://github.com/carrierwaveuploader/carrierwave#providing-a-default-url . – elgalesmana Mar 27 '18 at 13:22
  • are you using device? if you set `after_create` at the `User` model, you could access the attributes with `self.avatar = uploaded_image` , `current_user` is not accesible in the model. – elgalesmana Mar 27 '18 at 13:33
  • Yes I just thought of that before commenting so I removed that comment! I am really close.... when I set the avatar myself with current_user.avatar = imgpath it works. However when I want it to be done after_create with self.avatar = imgpath it doesnt work? – Pimmesz Mar 27 '18 at 13:50
  • My if statement was incorrect!!! It was `if self.avatar?` instead of `if !self.avatar?` . Everything fixed!! Thank you so much! – Pimmesz Mar 27 '18 at 13:53
1

You don't need to work with a form of default image when user not uploaded any image. You can just use a static image when user's avatar is empty. Just call out a default image

The right way to the set using user default image, you can create a helper method to any helper file like helpers/application.html.erb

def avatar_for(user)
    @avatar = user.avatar
    if @avatar.empty?
        @avatar_user = image_tag("user.png", alt: user.name)
    else
        @avatar_user = image_tag(@avatar.url, alt: user.name)
    end
    return @avatar_user
end

if user.avatar is empty then it will show the default user.png from assets/images folder otherwise it will show a user's uploaded the image

and put the image user.png to assets/images/ folder

Then you can just callout from .html.erb file like this

<%= avatar_for(current_user) %>
or
<%= avatar_for(@user) %>
#just pass user object from anywhere

Or if you need to show the image with different sizes for a different place then it would be like this

def avatar_for(user, width = '', height = '')
    @avatar = user.avatar
    if @avatar.empty?
        @avatar_user = image_tag("user.png", alt: user.name, width: width, height: height)
    else
        @avatar_user = image_tag(@avatar.url, alt: user.name, width: width, height: height)
    end
    return @avatar_user
end

Then callout like this

<%= avatar_for(current_user, 100, 100) %>

Or you can use gravatar for default avatar

def avatar_for(user)
    @avatar = user.avatar
    if @avatar.empty?
        gravatar_id = Digest::MD5::hexdigest(user.email).downcase
        @avatar_user = "https://gravatar.com/avatar/#{gravatar_id}.png"
    else
        @avatar_user = image_tag(@avatar.url, alt: user.name)
    end
    return @avatar_user
end

You can see the full tutorial for generating gravatar avatar image from RailsCast

fool-dev
  • 7,671
  • 9
  • 40
  • 54
  • So I have to create this helper file first correct? I found another file which is called application_helper.rb, could I put this code in there as well? Thanks! – Pimmesz Mar 27 '18 at 10:12
  • You don't need to create another one helper file, you can this with which helper file already exists like `application_helper.rb` – fool-dev Mar 27 '18 at 10:14