Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: JS Controller Being Called Twice for Some Reason

For some reason when I click on a button, my controller and the resulting jquery function gets called twice. Since the js function being called is toggle, this is a problem as it causes the code to jump in and out of view.

Here is the form:

Unseen Notifications: <%=  current_user.notification_unseen %> </div>
                <%= button_to "show", {:action => "seen", :controller=>"notifications"}, :remote => true %>

Here is the controller:

def seen
    respond_to do |format|
      format.js 
    end
  end

Here is the jquery:

$("div#notifications").toggle();
$("div#count").html("<%= escape_javascript( render :partial => 'notifications/count')%>");

I am at a loss for why on earth this would possibly happen. I have been very careful not to double click and don't know how to prevent it. Any suggestions would be greatly appreicated.

like image 373
Spencer Avatar asked Jul 12 '11 03:07

Spencer


1 Answers

I had a recent problem like this before, when I had 2 jquery libraries included. In particular, check that you only have jquery_ujs.js and not jquery.js. Seems like when you include both of them certain js functions are called twice.

Hope this helps!

like image 171
Benjamin Tan Wei Hao Avatar answered Oct 20 '22 22:10

Benjamin Tan Wei Hao