Search code examples
ruby-on-railssimple-formsimple-form-for

How can I set a default image for user in Rails


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 %>

Solution

  • 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?