Getting undefined local variable or method using Action Cable calling class method from model, rails
I am getting a error using action cable,
NameError (undefined local variable or method `connections_info' for MicropostNotificationsChannel:Class):
app/channels/micropost_notifications_channel.rb:12:in `notify'
app/models/notification.rb:8:in `block in <class:Notification>'
app/controllers/likes_controller.rb:11:in `create'
Rendering C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb
...
To my knowledge and from using controllers and models I can call the class method after_commit -> {MicropostNotificationsChannel.notify(self)}
and then get to the self.notifiy(notification)
and then as connections_info
is a instance method I should be able to call it inside that class and carry out my code but I get the error here?
class Notification < ApplicationRecord
...
after_commit -> {MicropostNotificationsChannel.notify(self)}
end
The action cable micropost notification channel
class MicropostNotificationsChannel < ApplicationCable::Channel
def subscribe
...
end
def unsubscribe
...
end
def self.notifiy(notification)
connection_results = connections_info
puts connection_results.inspect
end
end
Channel.rb
module ApplicationCable
class Channel < ActionCable::Channel::Base
def connections_info
# do stuff here
end
end
end
Solution 1:
You've defined connections_info
as an instance method in ApplicationCable::Channel
, but notify
is a class method and so it's looking for methods at the class level instead of the instance level. Sometimes classes will get around this by using method_missing
, but it doesn't look like Action Cable does that at a glance. Without knowing more about what you're trying to do, it's hard to say whether to change connections_info
to a class method, notify
to an instance method, or something else.