【问题标题】:Rails 5 Action Cable Not WorkingRails 5 动作电缆不工作
【发布时间】:2018-04-20 13:33:19
【问题描述】:

我查看了多个教程,当我创建新的消息记录时,似乎无法接收来自操作电缆的通知。我要做的就是在控制台中打印出响应,以便我知道它有效。我已经安装了redis,但仍然没有运气。这是我的文件。任何帮助,将不胜感激。

cable.yml

redis: &redis
adapter: redis
url: redis://localhost:6379/1

production: *redis
development: *redis
test: *redis

应用程序.js

//= require jquery
//= require bootstrap-sprockets
//= require rails-ujs
//= require turbolinks
//= require_tree ./channels

cable.js

// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
//
//= require action_cable
//= require_self
//= require_tree ./channels

(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer();

}).call(this);

messages.js

//= require cable
//= require_self

(function() {
    // Subscrive to the class name of the channel
    App.messages = App.cable.subscriptions.create('MessagesChannel', {
    /**
     * Whenever this channel pushes content, it is received here
     */
    received: function(message) {
        console.log(message);

    }
  });
}).call(this);

频道/消息.rb

class MessagesChannel < ApplicationCable::Channel
  def subscribed
    stream_from 'messages'
  end
end

models/message.rb

class Message < ApplicationRecord

    after_save :broadcast_save

    def broadcast_save
       ActionCable.server.broadcast 'messages', html: render_task
    end

    private
    def render_task
       ApplicationController.render(partial: 'messages/message', locals: { message: self })
    end
  end

application.html.erb 头部元素

 <head>
    <title>Test</title>
    <%= action_cable_meta_tag %>
    <%= csrf_meta_tags %>
    <%= javascript_include_tag "//static.twilio.com/libs/twiliojs/1.2/twilio.min.js" %>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

  </head>

出于测试目的,我只是在调用控制器操作时创建一条新消息。

class PhoneController < ApplicationController

    def index
        @tickets = []
        Message.create(body: "Test")
    end
end

rails 的控制台输出

Started GET "/phone" for 75.65.92.24 at 2017-11-08 00:54:15 +0000
Cannot render console from 75.65.92.24! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PhoneController#index as HTML
   (0.1ms)  BEGIN
  SQL (0.7ms)  INSERT INTO "messages" ("body", "created_at", "updated_at") 
VALUES ($1, $2, $3) RETURNING "id"  [["body", "Test"], ["created_at", "2017-
11-08 00:54:15.192006"], ["updated_at", "2017-11-08 00:54:15.192006"]]
  Rendered messages/_message.html.erb (0.5ms)
[ActionCable] Broadcasting to messages: {:html=>"asdf"}
   (5.4ms)  COMMIT
  Rendering phone/index.html.erb within layouts/application
  Rendered phone/index.html.erb within layouts/application (0.8ms)
Completed 200 OK in 77ms (Views: 60.7ms | ActiveRecord: 6.3ms)

redis 控制台输出(最后一行)

[37939] 08 Nov 00:49:19.158 * The server is now ready to accept connections on port 6379

【问题讨论】:

    标签: ruby ruby-on-rails-5 actioncable


    【解决方案1】:

    看来你的问题出在这里:

    无法从 75.65.92.24 渲染控制台!允许的网络:127.0.0.1、::1、127.0.0.0/127.255.255.255

    您可能会发现这个问题很有帮助:

    How to disable "Cannot Render Console from..." on Rails

    【讨论】:

      猜你喜欢
      • 2018-07-29
      • 2017-05-27
      • 2017-05-11
      • 2016-11-23
      • 2021-01-28
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多