代码之家  ›  专栏  ›  技术社区  ›  Lee Eather

使用Action Cable从模型rails调用类方法获取未定义的局部变量或方法

  •  0
  • Lee Eather  · 技术社区  · 6 年前

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

    据我所知,通过使用控制器和模型,我可以调用class方法 after_commit -> {MicropostNotificationsChannel.notify(self)} self.notifiy(notification) 然后作为 connections_info

    class Notification < ApplicationRecord
      ...
      after_commit -> {MicropostNotificationsChannel.notify(self)}
    end
    

    最新通知频道

    class MicropostNotificationsChannel < ApplicationCable::Channel
    
      def subscribe
        ...
      end
    
      def unsubscribe
        ...
      end
    
      def self.notifiy(notification)
        connection_results = connections_info
        puts connection_results.inspect
      end
    end
    

    频道.rb

    module ApplicationCable
      class Channel < ActionCable::Channel::Base
    
        def connections_info
          # do stuff here
        end
      end
    end
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Max    6 年前

    你已经定义了 connections_info 作为中的实例方法 ApplicationCable::Channel ,但是 notify method_missing ,但它看起来不像行动有线电视做到这一点一目了然。如果你不知道你想做什么,很难说是否要改变 连接\u信息 一个实例方法,或者别的什么。