-
Notifications
You must be signed in to change notification settings - Fork 382
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
link_to_add_association generating duplicates issue #207
Comments
Ok upon testing i have discovered its nothing to do with the cocoon logic, its simply the face that $(document).on('click', '.add_fields', function(e) is being called multiple times for single click. I am not too sure how to fix it though |
Ok so if i remove //= require turbolinks then the issue is fixed. So how do i get this working with turbolinks |
Please check issue #177 That should answer your question. Cocoon is compatible with turbolinks. Turbolinks reloads the page content, so if you include the javascript ( Does that make sense? |
Only in development? Not using turbolinks? Define "similar" then :) |
My question exactly. Why development? It had worked fine previously. |
Good news! Tips for the future: do not do |
OMFG! I don't know how long I've been looking for this answer. Thank you all soooo omuch! Had a feeling Turbolinks may have been at fault. |
Hi. I have cocoon working well on a form if i refresh the page. However, I get duplicates for the link_to_add_association for every page i have clicked on since the last refresh.
For example. If i go to a the products/new action by entering the url in a browser, the link_to_add_association works beautifully and generates one partial. But if i go to the products/index page and click on the the new_product_path link the link_to_add_association will generate 2 partials. If i click on the product_path link 10 times then click on the new_product_path the link_to_add_association will create 10 partials.
I am using rails 4 with turbolinks, foundation and simple form. Here is the code I am using
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
mount_uploader :image, ImageUploader #carrier wave
end
class Product < ActiveRecord::Base
has_many :images, :as => :imageable
accepts_nested_attributes_for :images, :reject_if => :all_blank, :allow_destroy => true
end
class ProductsController < ApplicationController
...
def product_model_params
params.require(:product).permit(:name, images_attributes: [:id, :image, :_destroy])
end
_form.html.erb
<%= simple_form_for @Product do |f| %>
<%= f.input :name %>
<%= f.simple_fields_for :images do |image| %>
<%= render 'image_fields', :f => image %>
<% end %>
<%= link_to_add_association 'add image', f, :images %>
<%= f.button :submit %>
<% end %>
// _image_fields.html.erb
<%= image_tag(f.object.image.url(:thumb)) if f.object.image? %>
<%= f.input :image, :as => :file %>
<%= link_to_remove_association "remove image", f %>
Any guidance would be highly appreciated
The text was updated successfully, but these errors were encountered: