Closed asgerb closed 7 years ago
Hi @asgerb I understand the issue I think.
How about making a branch, and adding the problematic setup to the test app (/test/dummy
) so we can see it and find a solid solution? (I have a hunch that something along the lines what you propose is what we'll end up with.)
I'm not currently allowed to push to the repo, should I make my own fork?
Sorry, updated the permissions so you should be able to work within this repo.
Great, pushed a test case to multiple-file-inputs-bug
Thank you, will check it out later today.
@asgerb I get error when booting up the app and opening at http://127.0.0.1:3000. Do you happen to know what am I doing wrong? Thanks!
Hmmm, not sure what the problem is, runs fine here – what's the error?
TestsController#new is missing a template for this request format and variant.
What happens if you go to http://127.0.0.1:3000/tests?
Ah my bad, I removed the extra Gemfile
and that caused the issue.
I pushed the update to your branch.
I see and remember running into this as well. I found these:
https://github.com/blueimp/jQuery-File-Upload/wiki/Multiple-File-Upload-Widgets-on-the-same-page https://github.com/blueimp/jQuery-File-Upload/wiki/Multiple-File-Input-Fields-in-One-Form
I think we should solve this for good, hmm …
As mentioned I think the default way in which rails creates the ids for form fields are flawed (not sure if simple_form
is doing smth different or just inheriting from rails here). They are simply doing something like "#{model_name}_#{field_name}"
, which obviously is no good if there are multiple instances of a model in the dom.
Anyways, I guess we just need to decide on the best way to generate unique ids. I was thinking smth like "#{parent_model_name}_#{parent_id}_#{child_model_name}_#{child_field_name}"
.
Then you have the case of new parents without an id... How to solve that I'm not sure.
OK, I added SecureRandom
to the field id. Seems to do the trick. I pushed the branch, can you test it in your real app please? Also dropping files, removing, saving form etc.?
BTW we could alternatively modify the ID from the JavaScript – which might be cleaner solution?
I think updating the IDs as a first thing in JS init
might solve the new parents issue you mention as well.
I pushed the JS solution. It does the same thing – but via JS. Take a look. Which one do you prefer?
Somehow I think that the issue is caused by JS, so in a way it make sense to again solve it via JS. ?
I prefer the ruby solution as I don't see how the issue is with JS. The id should ideally be unique from the get go. Even if we completely remove the JS (at least ours) from the equation, the issue remains.
Yeah, but would not we have to then rewrite all Rails form generators?
The issue is caused by JS, since the jquery uploader somehow does not bind to specific element, but refers via its ID. I say let's go with the JS solution as it addresses problem that's coming out of the uploader JS. That way it is also nicely coupled with the JS that inits the file uploader.
In any case, could you please test it thoroughly in your app? Feel free to make PR then.
Just did a quick codepen to show how the issue is not with the js. Try clicking on the second label (yellow element) and add a file.
works as expected in both Safari and Chrome ?
Great, then perhaps the whole can be solved by me updating Chrome (on version 61.0.3163.100) 😄
Hmm, in both Safari and Chrome (updated) on my computer it attaches the files to the first file input when clicking the second label.
Ah you're right — i did not know the yellow thing is a label … Still i wonder whether we should interfere with Ruby – I guess they'll fix it eventually?
Unfortunately I think it's a bit more complex. We are using template
(I guess this is a wrapper for an ActionView
instance?) to create all the fields, but this knows nothing of the "context", i.e. what form we are inside, who is the owner etc. @builder
on the other hand is an instance of SimpleForm::FormBuilder
and knows about the context. Using that might be the correct way to build the fields in a more dynamic and unique way, but it will require us to change the code quite a bit.
Hmm, you might be right. Still, though, I think we'll end up with the same id. Shall we sleep in it?
Yeah I'm also not sure it completely solves our issue 😢 Let's sleep on it and look at it with fresh eyes tomorrow 🙂
I think no matter how we get there (Ruby or JS) it is important to just test whether these unique ids do solve the problem (and do not cause any other). If so we can flip the coin ;-).
Still I somehow think this is more of a front-end issue and would prefer to leave the Rails forms generated as they are and modify them later via JS.
But … let's sleep :-).
Thanks Asger!
Hey hey Tomas,
So after sleeping on it a bit I think changing the forms to use @builder
will not fix the issue completely and I actually think it makes sense for the fields to be generic and only refer to attachment
, instead of the actual relation. So I think we should just do it via js
and keep the issue front-end as you say. Since we're only changing the id (and this is not submitted in the form), it should have no effect, but I will test it out on a few apps to make sure.
Hi Asger, yes I agree. Test it out please and when ready submit a PR and let's do it! Thanks for this! T
fixed via #22
Hey hey Tomas, I noticed a bug recently when having multiple attachment inputs open on a page (Chrome, haven't tried other browsers yet).
What happens is that clicking the "Choose file" button (or on the containing label element) triggers the very first attachment file input on the page. So when you click and select a file in any input but the first, the file gets added to the very first file input on the page.
The problem is that the
input
'sid
is the same for all inputs:attachment_file
. Thelabel
'sfor
attribute is therefore alsoattachment_file
across all labels. From an html standpoint this is also not valid – everyid
should be unique – but that is a problem in rails forms in general.I think the best way to solve this would be to amend the
attachment_file_field
method and give the file input a more specificid
(see below), but I wanted to get your thoughts on it first...My solution doesn't solve the situation where we have two "identical" attachment inputs open (meaning with similar parent/child relations, like two image modules for instace), where the bug would occur as well, seeing as the
id
would be the same for both inputs. One way to solve that would be to make theinput
'sid
field contain the id of the parent.