Ask A Question

Notifications

You’re not receiving notifications from this thread.

Disabling ActionCable for Unauthenticated Users Discussion

Hey Chris,
I tried to integrate the meta tag if else statement into the group chat app in the channels/chatrooms.js and got "cannot read property `send_message` of undefined" error. It was surprising since if I checked the val of the meta length it returned 1. It took some time to realize that you have to wrap this code into a `turbolinks:load` event since when the code gets to that point when send_message is invoked the meta tag length value is still 0.

Reply

in js files:

$(document).on("turbolinks:load", function () {
if ($("meta[name='current-user']").length >0) {

[...]

}
});

in coffee files:

$(document).on 'turbolinks:load', ->
**if $("meta[name='current-user']").length >0

[...]

(the ** only indicate mandatory indentation - they need to be removed and replaced by spaces)

Reply

Hi Chris,
How would you add this to your chatrooms.coffee.erb or a last_read.coffee from the chat application? Not sure how to translate it in coffee script.
Stan

Reply

This tool can translate to and from Coffeescript: http://js2.coffee/ 👍

Reply
Does anyone have a working coffee version of the project files t. I'm unable to get "an unauthorized attempt..." to stop even though I've made the changes. When the user isn't on the chatrooms_controller, it shouldn't be searching for that user. Please, assist me -- I've been working on this thing for a few weeks now.
Reply
I think its finally working. I'm no longer receiving the message while in other controllers.
Reply

Hi!

What should I do if I want some channel work with current_user and some channels work with Unauthenticated user (just visitors)? How do I modify channels/application_cable/connection.rb or channels/some_channel.rb ?

Is it possible?

Thank you very much!

Reply

Is this method supposed to work with the new way of handling JS in Rails through Webpacker?

Can't get it to work I am assuming because the files no longer have view of the metatag on the page and so the if statement always returns false...

Thanks!

Reply

Got this to work by putting it above the JS pack tag in the head and wrapping the if statement around the require("channels"); in our appication.js ...

Reply

Great. What would be the recommended approach to achieve the same when using an API-only app?

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.