0

I have an application that uses paperclip to bind users and their avatars, I have a problem when I edit a user profile because the button to upload the image says "You didn't select any file" when it is the case, if I change the file_field type by text_field then I have the url of the file, how do I get it back automatically in the label?

contact.haml

-# Profile picture
    = form.label :image, t("settings.profile.profile_picture")
    #img_preview
    = image_tag(@current_user.image.url(:thumb), id: "user_avatar") if @current_user.image
    = form.file_field :image, :size => 30, :id => "avatar_file"

person.rb

has_attached_file :image, :styles => {
                      :medium => "288x288#",
                      :small => "108x108#",
                      :thumb => "48x48#",
                      :original => "600x800>"}

  process_in_background :image

  #validates_attachment_presence :image
  validates_attachment_size :image, :less_than => 9.megabytes
  validates_attachment_content_type :image,
                                    :content_type => ["image/jpeg", "image/png", "image/gif",
                                      "image/pjpeg", "image/x-png"] #the two last types are sent by IE.
5
  • Can you show your the code? Commented Feb 6, 2018 at 12:31
  • All right, I edited my question. Commented Feb 6, 2018 at 13:02
  • If you want to get image name, you should use 'image_file_name' column for paperclip gem. Commented Feb 6, 2018 at 13:27
  • yes but it would be cleaner than if the user already has an image it should see the name of its file instead of seeing "No selected file". Commented Feb 6, 2018 at 13:44
  • Okay, I understood it now. As far as I know, you can't do that. Commented Feb 6, 2018 at 13:58

1 Answer 1

1

Answer V2:

For security reasons, it seems you can't directly assign a value to a file field; therefore, the simplest way to do it is to adjust the labelling based on the presence of an image.

So, I presume your unwanted message comes from t("settings.profile.profile_picture"). To resolve this, you

-# Profile picture
  - if @current_user.image.present?
    = form.label :image, @current_user.image.url(:thumb) # Or whatever text you want 
  - else
    = form.label :image, t("settings.profile.profile_picture")

  #img_preview
  = image_tag(@current_user.image.url(:thumb), id: "user_avatar") if @current_user.image
  = form.file_field :image, size: 30, id: "avatar_file", value: @current_user.image&.attachment&.url

Does that seem like what you're looking for here?

Sign up to request clarification or add additional context in comments.

3 Comments

No, I thank you but the problem remains the same but in fact it comes from "simple_form" and not from paperclip.
@JeanClaudeLemens - I've updated my answer based on a little more digging around. It seems you can't assign a value to the file_field, so common practice is to conditionally change the content of the label. Does this seem more like what you're after?
Yes, that's what I thought I understood by looking a little bit more but I finally found an interesting solution by following these instructions : stackoverflow.com/questions/14509449/… Anyway, thank you very much for your help; -))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.