-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opening form with multiple empty child records and duplicate values from #75
Comments
@erwin, what is the context of your request? when you are clicking I'm not super familiar with the gem but to me, the Rails way to load nested attributes is at the controller side, like so: class User
has_many :items
end
class Item
belong_to :user
end
class UsersController < ApplicationController
def new
@user.new(items: [Item.new, Item.new, Item.new])
end
end then in the frontend you should be loading the nested attributes somehow pass the Does that make sense to you? |
@JuanVqz thank you very much. I had completely missed that somehow. I updated my controller to use logic like you suggest:
And then updated my https://guides.rubyonrails.org/form_helpers.html#dealing-with-model-objects Thanks so much! |
nice to know it worked for you @erwin! If you are happy with the result, can you please close the issue? |
thanks @JuanVqz! that is the intended way, yes |
What is the proper way to preload some empty vanilla-nested rows in a form?
Right now, I'm simply calling he
.click()
method onbutton.vanilla-nested-add
. I call it 3 times in a row to get three empty records as a default layout for my form. Unfortunately, my input elements often get duplicate names.Using document.querySelector("button.vanilla-nested-add").click();
I think the issue with my way of doing this, is in
vanilla_nested.js
, I thinkDate.now()
is used to help dynamically rename the input fields, but when I call this function three times in a row, Date.now often returns the same value. I also tried to substituteperformance.now()
instead of Date.now, but I still get duplicates sometimes.https://github.com/arielj/vanilla-nested/blob/cab8f390dc99eed933952f6fcc14603bad30c6a8/app/assets/javascripts/vanilla_nested.js#L10C1-L10C73
Test for duplicate with Date.now via:
https://jsfiddle.net/2rfuzvbj/
Compare Date.now with performance.now via:
https://jsbench.me/hploqs196b/
I'm probably doing it completely wrong... So if you have any tips on how to get multiple empty rows to load as the form loads please let me know!
The text was updated successfully, but these errors were encountered: