I have a weird problem where JQuery is creating two AJAX requests for a link_to method. I am developing a Rails 3 app with JQuery for UJS. I have a toggle link which toggles between 'Follow' and 'Unfollow'
My link is rendered as below:
<span id="follow_link"> <a href="/tfollow_artist?id=8103103" data-method="post" data-remote="true" id="follow_artist" rel="nofollow">Unfollow</a> </span>
and my controller is setup so:
def tfollow_artist @artist = Artist.find(params[:id]) if current_user.following?(@artist) current_user.stop_following(@artist) else current_user.follow(@artist) end end
which finally renders a js as:
$('#follow_link').html('<%= escape_javascript(render :partial => "follow") %>');
Which essentially replaces html contents of the '<span id="follow_link">...</span> with the same URL only with the text being different. For example, above will now be rendered as:
<span id="follow_link"> <a href="/tfollow_artist?id=8103103" data-method="post" data-remote="true" id="follow_artist" rel="nofollow">Follow</a> </span>
However this is somehow causing JQuery to make two AJAX requests.
Can any one see what is wrong here?
I am using 'jquery-rails' gem which is copying the latest jquery-ujs file to my app. JQuery version is 1.4.3
Thanks to this dialog:
https://github.com/rails/jquery-ujs/issues/208
I was able to discover that jquery and jquery_ujs were getting included twice.
I guess the jquery-rails gem automatically puts them into application.js, and then I had them included in application.js as well.
Seems like for whatever reason application.js automatically bundles everything in app/assets/javascripts/ - even when I remove all the requires.
So, if your :remote => true forms are getting submitted twice, try checking application.js.
Hope this helps!
UPDATE: I believe this might have had something to do with me pre-rendering my assets without using a digest, so then when my development environment adds script tags in the html head dynamically from the requires in app/assets/application.js, it also adds one for what should be an empty dynamic application.js, except the static one from public/assets gets loaded. Confusing? Yep!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With