ActionCable NoMethod 错误 [英] ActionCable NoMethod Error

查看:36
本文介绍了ActionCable NoMethod 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的玩具导轨应用程序中添加了简单的 ActionCable 并发现我得到了

I've added simple ActionCable to my toy rails app and discovered that I'm getting

错误:ProductsControllerTest#test_should_update_product:NoMethodError: ApplicationCable:Module 的未定义方法 server'app/controllers/products_controller.rb:53:inblock in update'

Error: ProductsControllerTest#test_should_update_product: NoMethodError: undefined method server' for ApplicationCable:Module app/controllers/products_controller.rb:53:inblock in update'

app/controllers/products_controller.rb:44:in `update'
test/controllers/products_controller_test.rb:44:in `block in <class:ProductsControllerTest>'

当我开始 Rails 单元测试时.这是唯一发生错误的时间.我的代码:

when I start my rails unit tests. It's the only time when error occurs. My code:

products_controller_test:

products_controller_test:

  test 'should update product' do
    patch product_url(@product), params: { product: @update }
    assert_redirected_to product_url(@product)
  end

config/application.rb

config/application.rb

require 'action_cable'

/app/channels/application_cable/channel.rb

/app/channels/application_cable/channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base
  end
end

/app/channels/application_cable/connection.rb

/app/channels/application_cable/connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
  end
end

/app/channels/products_channel.rb

/app/channels/products_channel.rb

class ProductsChannel < ApplicationCable::Channel
  def subscribed
    stream_from 'products'
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

/app/assets/javascripts/channels/products.coffee

/app/assets/javascripts/channels/products.coffee

App.products = App.cable.subscriptions.create "ProductsChannel",
  connected: ->
    # Called when the subscription is ready for use on the server

  disconnected: ->
    # Called when the subscription has been terminated by the server

  received: (data) ->
    $(".store #main").html(data.html)

/app/controllers/products_controller.rb

/app/controllers/products_controller.rb

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html do
          redirect_to @product, notice: 'Product was successfully
          updated.'
        end
        format.json { render :show, status: :ok, location: @product }

        @products = Product.all
        ApplicationCable.server.broadcast 'products',
                                          html: render_to_string('store/index',
                                                                 layout: false)
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

感谢您的帮助!

推荐答案

在 products_controller.rb 中.您应该使用ActionCable.server.broadcast"而不是 ApplicationCable.server.broadcast

in products_controller.rb. You should use 'ActionCable.server.broadcast' instead of ApplicationCable.server.broadcast

玩得开心!

这篇关于ActionCable NoMethod 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆