首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionCable NoMethod误差

ActionCable NoMethod误差
EN

Stack Overflow用户
提问于 2017-04-18 01:01:56
回答 1查看 821关注 0票数 0

我在我的玩具rails应用程序中添加了简单的ActionCable,并发现

错误: ProductsControllerTest#test_should_update_product: NoMethodError:更新中未定义的方法server' for ApplicationCable:Module app/controllers/products_controller.rb:53:in块 app/controllers/products_controller.rb:44:in update' test/controllers/products\_controller\_test.rb:44:inblock in ‘

当我启动rails单元测试时。这是唯一发生错误的时候。我的代码:

products_controller_test:

代码语言:javascript
复制
  test 'should update product' do
    patch product_url(@product), params: { product: @update }
    assert_redirected_to product_url(@product)
  end

config/application.rb

代码语言:javascript
复制
require 'action_cable'

/app/信道/应用程序_电缆/信道

代码语言:javascript
复制
module ApplicationCable
  class Channel < ActionCable::Channel::Base
  end
end

/app/信道/应用程序_电缆/connection.rb

代码语言:javascript
复制
module ApplicationCable
  class Connection < ActionCable::Connection::Base
  end
end

/app/channels/products_channels.app

代码语言:javascript
复制
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

代码语言:javascript
复制
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/控制器/产品_Controller.rb

代码语言:javascript
复制
  # 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

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-13 16:37:24

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

玩得开心!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43461917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档