Skip to main content

cancel actioncable connection for unauthorized users?

Rails • Asked by Matt Taylor

Followed the series on action cable notifications and setup the meta tag so that action would not try to connect if the meta tag was present. Here is my code.

$(document).on("turbolinks:load", function () {
  if ($("meta[name='current_user']").length > 0) {
    App.activity_feed = App.cable.subscriptions.create("ActivityFeedChannel", {
    connected: function() {console.log("I connected")},
    disconnected: function() {console.log("I disconnected")},
    received: function(data) {
      $("#completed-timeline-ul").prepend(data.html);
    }
    });
  };
});

I tried it with and without the turbolinks code as well.. no dice.
I look in the dev tools and see that the meta tag is present and when I run $("meta[name='current_user']").length > 0 in the console it comes back as true.

Not sure what i'm missing. If i get rid of the if statment actioncable connects successfully.

Thanks for the help.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.