cocoon: Rails 5 + Turbolinks - Clicking add new *thing* duplicates.
I’m using this with Rails 5 on my recipe box app. It creates multiple ingredients when clicking the button once. It will randomly create 3, 6, 9 of them - instead of just one. I could be calling it wrong?
= link_to_add_association 'Add Ingredient', f, :ingredients, class: "btn btn-default add-button"
I have my application.js
call in the header and //= require turbolinks
is near the bottom.
You can see it here…
https://github.com/sbramlett/recipe_box/blob/master/app/assets/javascripts/application.js
https://github.com/sbramlett/recipe_box/blob/master/app/views/layouts/application.html.haml
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 17 (3 by maintainers)
No apologies needed, I think it is awesome you were trying to help! 👍
My bet on your problem … you are loading turbolinks twice. I’m honestly not sure if that’s the entire fix - you might have to have the above code I included too. The other thing to, if you are running on stackoverflow - there’s a ton of different opinions on if you should do both jquery & jquery-ujs. in the
application.js
.Note: The other alternative, I couldn’t remember…you can also load jquery as a CDN now … it’s pretty slick way to do bootstrap too. There’s a really good break down of why & how to use the application.js versus the application.html.haml here … Link
You have it once in the
app/assets/javascript/application.js
…Here again … in the
app/view/layouts/application.html.haml
…Yeah @Mirv - I appreciate the assistance. Best way to learn is to jump in with the sharks. 😄
@nathanvda you can close this issue now. Thanks again!
@sbramlett Hi, I was following along & I had a similar issue with the callbacks not setup correctly. I know in the formtastic demo Nathan doesn’t populate the callbacks, but in the simpleform demo he does.
Here is a copy of the fix where Nathan corrected my callbacks … Github Link.
I notice you don’t have any callbacks via javascript listed & not sure if that will actually help or not but thought I’d mention it was one of the final fixes before my test project got rid of the clicking to add a field would give me randomly give me extra fields.