【发布时间】:2017-01-31 08:06:07
【问题描述】:
我知道,有很多“参数数量错误”之类的问题,但我的不同,因为我正在尝试使用动作电缆添加实时功能。
我真正想要完成的是,一旦有新项目,项目列表应该在另一个浏览器上自动更新。
应用部署在 http://rails5-catalog.herokuapp.com
https://catalog-tenzan.c9users.io/items
Github 仓库https://github.com/tenzan/rails-catalog
我遇到了错误:
当我尝试在def self.broadcast(item) 处为item 添加@ 时,我遇到了另一个错误formal argument cannot be an instance variable。
我做了什么:
rails g channel items
app/assets/javascripts/channels/items.coffee
App.items = App.cable.subscriptions.create "ItemsChannel",
received: (data) ->
# Called when there's incoming data on the websocket for this channel
$('#items').append data.item
app/channels/items_channel.rb
class ItemsChannel < ApplicationCable::Channel
def self.broadcast(item)
broadcast.to item, item:
ItemsController.render(partial: 'items/form', locals: {item: item})
end
def subscribed
stream_for Item.last
end
app/controllers/items_controller.rb
def create
@item = Item.new(item_params)
ItemsChannel.broadcast(@item)
redirect_to @item
config/routes.rb
Rails.application.routes.draw do
mount ActionCable.server => '/cable'
【问题讨论】:
-
我没有使用过动作电缆,所以我的猜测是,这是因为您正在向
new Object广播,而不是持续广播。而且所有人都订阅了Item.last,所以我猜他们不会收到更新,除非你订阅他们到一个更具体的频道实例,因为你不总是广播到那个特定的Item -
是的,我在您的
create操作中看不到@item.save。它是如何与动作电缆一起工作的吗?我不确定。 -
感谢大家的宝贵时间。根据我在下面的评论,错误已被消除,但我现在面临可操作的使用问题。一有发现就会更新。
标签: ruby-on-rails ruby ruby-on-rails-5 actioncable