Combination with cocoon gem: Upload of base64 string works for creating a nested object, but not for updating it
jmuheim opened this issue · 6 comments
In my project I figured out that your gem works perfectly in normal forms: I can easily upload base64 strings using a textarea, and your gem transforms it into a file. This works for both creating and updating objects.
I'm using https://github.com/nathanvda/cocoon gem which allows me to manage nested objects within the form of a parent object. Using it, it seems still to be possible upload a base64 string when creating a nested object, but when updating it, it doesn't work: it simply keeps the originally uploaded file.
I have created a test Rails app, so you can check it out: https://github.com/jmuheim/test-carrierwave-base64-update
Simply start the Rails server, and play around on localhost:3000/users
:
- Create a user and upload both a normal file and a base64 string file (I have added two base64 image strings to the form so you can simply copy&paste them)
- Update the user and change both the normal file and the base64 string file => this works nicely
- Now add an attachment to the user (click "add attachment" in the user form)
- Do the same thing: add a normal and a base64 string file => works perfectly
- Then try to update the attachment with a new file and a new base64 string => this works only for the normal file, not for the base64 file!
It would be really nice to get this to work. Any suggestion? I tried to debug, and all I can say is that it looks to me like everything seems to be passed perfectly from the form to the model, all the params look okay to me, and the base64 string seems to be passed properly to the nested object, too. But somehow it's not really saved to the disk... No idea what could be the difference between a normal and a nested update of attributes, and why create and update should be any different.
Thanks a lot for help! Tell me if I can support you in some way (e.g. writing pending specs for my dummy app which you could try to make pass).
I have added model specs, and in the specs, the update works as expected.
jmuheim/test-carrierwave-base64-update@bd6a409
So it seems it's not a problem on the model's level.
I will try to add feature specs, showing the problem...
I added feature specs which I hoped would show the problem, but somehow it seems to work in the specs, but not in the "live" browser, see here:
As @nathanvda, the creator of the cocoon gem, helpfully pointed out: it seems that when only setting the base64 string to a new string and no other attribute of the nested object, no update happens. But when in addition to the base64 string another attribute of the nested object is changed, then the update happens as expected, even for the base64 string:
nathanvda/cocoon#311 (comment)
So it seems to be some problem with ActiveRecord not being notified of an attribute change when only the base64 string was changed. You can reproduce this behaviour easily in the browser:
- Create a user with an attachment
- Edit the user:
- When you only set the base64 string to a new one, it won't be saved as a new image
- When you set the base64 string to a new one and also set a new name, the name and the base64 string are saved
I have no idea why in the specs it works but not in the "live" browser. Any help fixing this problem is highly appreciated. Thanks.
I'll be on holidays the next week. It would be very nice to see some progress here until then, as our application heavily depends on your gem in combination with nested attributes and cocoon.
If I can be of any help afterwards, please ask. Thank you.
I will take a look in a couple of days, had no time yet
On Fri 28 Aug 2015 at 15:51 Joshua Muheim notifications@github.com wrote:
I'll be on holidays the next week. It would be very nice to see some
progress here until then, as our application heavily depends on your gem in
combination with nested attributes and cocoon.If I can be of any help afterwards, please ask. Thank you.
—
Reply to this email directly or view it on GitHub
#16 (comment)
.
It's great to hear that you're planning to take care of it. I'm very eager to know where exactly the problem lies. Be sure to take also a look at the corresponding ticket at cocoon's side:
Best wishes.
The problem was that the attribute was not marked as updated by the carrierwave
gem. Your test app works as expected with the new version of carrierwave-base64
.
Sorry that it took a while for me to fix it, i was on vacation the last week.