【问题标题】:ActionCable NoMethod ErrorActionCable NoMethod 错误
【发布时间】:2017-04-18 01:01:56
【问题描述】:

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

错误: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 单元测试时。这是唯一发生错误的时间。 我的代码:

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

require 'action_cable'

/app/channels/application_cable/channel.rb

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

/app/channels/application_cable/connection.rb

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

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

  # 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

感谢您的帮助!

【问题讨论】:

    标签: ruby ruby-on-rails-5


    【解决方案1】:

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

    玩得开心!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多