All threads / Disabling ActionCable for Unauthenticated Users Discussion

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.

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

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

yes. was my immediate attempt but...still no success.

What are you having trouble with? The if statements around your code should be mostly the same.

I added
if $("meta[name='current-user']").length >0
above the
App.chatrooms = App.cable.subscriptions.create "ChatroomsChannel"

but this leads to
SyntaxError: [stdin]:1:1: unexpected if

Can you share your whole file?

For readers of these comments, the issue was with the indentation. Beware of coffeescript on that side! Thks Chris again for your help.

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.
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!

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
Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.