0

The goal: User selects either a avatar image for upload and clicks "Upload" or clicks "Reset" to remove the custom avatar image and use default. (The underlying forms, controller, action, model are the same except for the "picture" field is hidden in the non-working form.)

1- When the user selects an image, the form submit works and carrierwave uploads the image. Here's the working version submit action hash:

<%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>
Hash {"utf8"=>"✓", "_method"=>"patch", 
"authenticity_token"=>"xxxxx", 
"avatar"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0x2e77fd8 
@tempfile=#<File:C:/DOCUME~1/Val/LOCALS~1/Temp/RackMultipart20150305-2620-
1pq50l5.png>, @original_filename="admin_foodler_profile.png", 
@content_type="image/png", @headers="Content-Disposition: form-data;
 name=\"avatar[picture]\"; 
filename=\"admin_foodler_profile.png\"\r\nContent-Type: image/png\r\n">},
 "commit"=>"Upload photo", "controller"=>"avatars", "action"=>"update", 
"user_id"=>"8"} 

2- When the user selects to replace avatar with default, here's the non-working submit action hash.

    <%= f.hidden_field :picture, value:'default_profile.png' %>
Hash {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"xxxx", 
"avatar"=>{"picture"=>"default_profile.png"}, "commit"=>"Reset profile image",
 "controller"=>"avatars", "action"=>"update", "user_id"=>"8"}

When I use the hidden field on the form with the default image name, as you see in the hash. "picture" is just the filename, it's not the upload method, but I don't know why. But as a result ActionDispatch is not invoked and the default image is not uploaded nor is the table updated.

I'm pretty sure there is a simple answer, and that the hidden attribute "picture" field is the culprit. But I'd really like to offer users the ability to simply switch their avatar for the default png. I thought this would be a no-brainer change. Hah. Suggestions much appreciated.

Elvn
  • 3,021
  • 1
  • 14
  • 27

1 Answers1

1

I think, that you should not implement default avatar at view level of your app.

Just add checkbox for using default avatar, that will remove file at controller level, like

@user.remove_avatar!

And then configure carrierwave uploader to use default url, like: https://github.com/carrierwaveuploader/carrierwave#providing-a-default-url

Stanislav Mekhonoshin
  • 4,276
  • 2
  • 20
  • 25
  • I'd like to know why ActionDispatch wasn't invoked on the submit. The uploader is mounted on the field, the form is submitted, but the processing doesn't happen. Resolving this one implementation is good, but knowing why it fails is important to me. I think that might come later. – Elvn Mar 05 '15 at 18:11
  • But, you did answer the question of setting the default image, so I think I should accept your answer...Thanks! – Elvn Mar 05 '15 at 18:14
  • It is not invoked, because you didn't actually send file to server, just a string with filename. If I answered your question, then feel free to mark my answer as accepted(green mark below answer rate) – Stanislav Mekhonoshin Mar 05 '15 at 18:57
  • Yes, but why did it not invoke the upload method on submit using the filename? That is the question. It remains unanswered. I still think it is some attribute of the hidden form field, but I don't know. – Elvn Mar 05 '15 at 19:19
  • There's a linkage on the avatar filename created when the user clicks on a filename in the Carrierwave pop-up, versus when I simply pass the filename. It has to be the file-picker dialog that connects the string filename to the upload action. I think this is the answer. The dialog that Carrierware triggers, is not triggered when I just pass a hidden filename into the submit. No doubt there is a direct method way to invoke that action but I am moving on. Thank you @Stanislav Mekhonoshin. (My great grandfather was Stanislaw.) – Elvn Mar 05 '15 at 19:45