ruby-on-rails - ActionCable 在操作后显示一个空页面

标签 ruby-on-rails actioncable

我正在关注迈克尔·哈特尔 tutorial关于 Rails 5 ActionCable,建立一个简单的聊天,我的 create 内部出现问题行动在 MessageController :

  def create
    message = current_user.messages.build(message_params)
    if message.save
      ActionCable.server.broadcast 'room_channel',
                                     content:  message.content,
                                     username: message.user.username
      head :ok
    end
  end

一直到head :ok ,然后显示一个空的 HTML 页面。日志是:
Processing by MessagesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"h9UsB78aX8mO8Nyn8eZEUuDzAOxL4V7UCMwNuTfwALliMv7OCkcUIeR4/xXIcvPjwq9H1bphjn6G96G+0VYisw==", "message"=>{"content"=>"oi"}, "commit"=>"Send"}
  User Load (1.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Message Load (2.1ms)  SELECT  "messages".* FROM "messages" ORDER BY "messages"."created_at" DESC LIMIT ?  [["LIMIT", 50]]
   (0.3ms)  begin transaction
  SQL (9.3ms)  INSERT INTO "messages" ("content", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["content", "oi"], ["user_id", 1], ["created_at", 2016-09-19 02:12:58 UTC], ["updated_at", 2016-09-19 02:12:58 UTC]]
   (9.1ms)  commit transaction
[ActionCable] Broadcasting to room_channel: {:content=>"oi", :username=>"alice"}
Completed 200 OK in 69ms (ActiveRecord: 22.0ms)


RoomChannel transmitting {"content"=>"oi", "username"=>"alice"} (via streamed from room_channel)
Finished "/cable/" [WebSocket] for 10.0.2.2 at 2016-09-19 02:12:58 +0000
RoomChannel stopped streaming from room_channel

(之后,我可以刷新页面并正确显示消息)

为什么会这样?

我的 RoomChanel 类(class):
class RoomChannel < ApplicationCable::Channel
  def subscribed
    stream_from "room_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end

end

我的房间.咖啡
App.room = App.cable.subscriptions.create "RoomChannel",
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) ->
  alert data.content

最佳答案

我知道答案已经被接受,但是这个解决方案对我有用,而不必删除 head :ok ,也许它可以帮助某人。

通常在开发环境中人们正在运行 localhost:3000但我在不同的端口上运行,localhost:3030 .

在这种情况下,我必须为该端口添加一个允许的请求源,如下所示:

config.action_cable.allowed_request_origins = [
      'http://localhost:3030'
  ]

到我的 /config/environments/development.rb

关于ruby-on-rails - ActionCable 在操作后显示一个空页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39564639/

相关文章:

ruby-on-rails - Ruby on Rails 全局数组

ruby-on-rails - 在 Rails 4 中配置延迟作业的异常通知

ruby-on-rails - ActionCable 未接收数据

ruby-on-rails - Rails API 应用程序中 ActionCable 的 Websocket 客户端

ruby-on-rails - Ruby on Rails - ActiveAdmin - 当 default_namespace = false 时配置实用程序导航

ruby-on-rails - Rails,渲染部分时,每个渲染都会在日志中输出。这是性能打击吗?

ruby-on-rails - RSpec:FactoryBot 看到重复的定义

ruby-on-rails - ActionCable Websocket 升级和 404 错误

ruby-on-rails - ActionCable - WebSocket 握手期间出错 : Unexpected response code: 404