How to access concerns and application controller code in Rails 5 Action Cable Channels
Asked Answered
M

1

6

I have some code in my concerns that I want to call from Action Cable Channel.

Say I have notification channel and I want to call my concerns to update some

values and it does call the method but it does not call the another method

written in application controller.

Say,

In my channel.rb I am using:

class NotificationsChannel < ApplicationCable::Channel
  include NotificationChannel

 def some_method
  create_notification(current_user, quiz)
 end
end

NotificationChannel Concerns code:

module NotificationChannel
  extend ActiveSupport::Concern

  def create_notification(is_current_user, resource)
    teacher_user_id = resource.lesson_plan.teacher.user_id
    Notification.create(description: "A user with name #{get_user_name(is_current_user)} has liked the  #{resource.title}", user_id: teacher_user_id, url: resource_misc_path(resource, "quiz_notification_like_check"))
  end
end

Now,

1st problem:

The method get_user_name(is_current_user) is not accessible in concerns and

it is written in ApplicationController.

2nd problem:

resource_misc_path(resource, "quiz_notification_like_check" is written in some helper it does not call even helper method.

P.S: I have also noticed the routes having _path and _url are also not accessible in Channel.rb

Any suitable workaround?

Messene answered 20/3, 2017 at 21:52 Comment(0)
P
0

There are some very useful answers on this topic here: How do I get current_user in ActionCable rails-5-api app?

The bottom line is in Cable there is no session, so you can't really use your session methods.

You have to rewrite them using directly the cookies.

As @Sajan says in his answer: "The websocket server doesn't have a session, but it can read the same cookies as the main app."

Prejudge answered 14/6, 2020 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.