0

i have a file_field on my form to upload user's avatars:

<%= f.file_field :avatar %>

The problem is, whether the model has already an avatar or not, the helper shows a "Not file selected" message. Only if you browse and add an image the message is replaced by the name of the selected image.

Basically, i'd like to show a message with the image name straight ahead if the user has already an image. Any ideas?

ntonnelier
  • 1,539
  • 3
  • 23
  • 49

1 Answers1

0

I'm sure you're saving the uploaded file in any way (carrierwave, paperclip etc.).
So you know whether you already have an avatar or not.
If you have one, show the file name or even a thumb, if not, show a note.

But you cannot avoid the "No file selected" message because it (aka the browser) sais:
There is no file selected to upload with this POST.

You will end up with somthing like:

<%- if f.object.avatar.present %>
  Known Avatar: <%= image_tag f.object.avatar.url(:thumb) %>
<%- else %>
  no Avatar uploaded yet
<%- end %>
<%= f.file_field :avatar %>

Remark:

I used a thumb url generating syntax of carrierwave. It may vary with other attachment gems.
I don't know your form object, that's why I used the f.object. If it's i.e. @user, you can simplify it:

<%- if @user.avatar.present %>
  Known Avatar: <%= image_tag @user.avatar.url(:thumb) %>
<%- else %>
  no Avatar uploaded yet
<%- end %>
<%= f.file_field :avatar %>
Martin M
  • 8,430
  • 2
  • 35
  • 53
  • Thanks @Martin. I'm using paperclip and i already show either the uploaded file or a default image. But i also need the helper there. So there's no way to avoid "No file selected" message? – ntonnelier Nov 26 '14 at 17:58
  • Use JS and CSS to build a custom entry field, see http://stackoverflow.com/questions/4909228/style-input-type-file – Martin M Nov 26 '14 at 18:04