Skip to content
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

Closed
CaseJnr opened this issue Apr 5, 2014 · 7 comments
Closed

link_to_add_association generating duplicates issue #207

CaseJnr opened this issue Apr 5, 2014 · 7 comments

Comments

@CaseJnr
Copy link

CaseJnr commented Apr 5, 2014

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

@CaseJnr
Copy link
Author

CaseJnr commented Apr 5, 2014

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

@CaseJnr
Copy link
Author

CaseJnr commented Apr 5, 2014

Ok so if i remove //= require turbolinks then the issue is fixed. So how do i get this working with turbolinks

@nathanvda
Copy link
Owner

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 (application.js or cocoon itself) in the body, you will get double clicks. You should always make sure js is included in the head and then it is only loaded once.

Does that make sense?

@nathanvda
Copy link
Owner

Only in development? Not using turbolinks? Define "similar" then :)

@rhuppert
Copy link

rhuppert commented Apr 2, 2015

My question exactly. Why development? It had worked fine previously.
It made no sense until I started poking around the public assets folder. I removed several files I thought might be suspect and that resolved the issue.
Thanks for the gem, by the way. Nice work.

@nathanvda
Copy link
Owner

Good news! Tips for the future: do not do rake assets:precompile in development, because I have noticed weird effects with that too. And to really clean the assets, you have to do rake assets:clobber (clean leaves the last three compilations and deletes the older ones).

@monkeysh1ne
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants